<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import argparse <EOL> import datetime <EOL> import json <EOL> import multiprocessing <EOL> import os <EOL> import random <EOL> import sys <EOL> import threading <EOL> import time <EOL> import numpy as np <EOL> from PIL import Image <EOL> import six <EOL> import six . moves . cPickle as pickle <EOL> from six . moves import queue <EOL> import chainer <EOL> from chainer import computational_graph <EOL> from chainer import cuda <EOL> from chainer import optimizers <EOL> from chainer import serializers <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:train>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = <NUM_LIT:32> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = <NUM_LIT:10> , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = - <NUM_LIT:1> , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = <NUM_LIT:20> , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT:.>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT:state>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> if args . gpu >= <NUM_LIT:0> : <EOL> cuda . check_cuda_available ( ) <EOL> xp = cuda . cupy if args . gpu >= <NUM_LIT:0> else np <EOL> assert <NUM_LIT> % args . val_batchsize == <NUM_LIT:0> <EOL> def load_image_list ( path , root ) : <EOL> tuples = [ ] <EOL> for line in open ( path ) : <EOL> pair = line . strip ( ) . split ( ) <EOL> tuples . append ( ( os . path . join ( root , pair [ <NUM_LIT:0> ] ) , np . int32 ( pair [ <NUM_LIT:1> ] ) ) ) <EOL> return tuples <EOL> train_list = load_image_list ( args . train , args . root ) <EOL> val_list = load_image_list ( args . val , args . root ) <EOL> mean_image = pickle . load ( open ( args . mean , '<STR_LIT:rb>' ) ) <EOL> if args . arch == '<STR_LIT>' : <EOL> import nin <EOL> model = nin . NIN ( ) <EOL> elif args . arch == '<STR_LIT>' : <EOL> import alex <EOL> model = alex . Alex ( ) <EOL> elif args . arch == '<STR_LIT>' : <EOL> import alexbn <EOL> model = alexbn . AlexBN ( ) <EOL> elif args . arch == '<STR_LIT>' : <EOL> import googlenet <EOL> model = googlenet . GoogLeNet ( ) <EOL> elif args . arch == '<STR_LIT>' : <EOL> import googlenetbn <EOL> model = googlenetbn . GoogLeNetBN ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if args . gpu >= <NUM_LIT:0> : <EOL> cuda . get_device ( args . gpu ) . use ( ) <EOL> model . to_gpu ( ) <EOL> optimizer = optimizers . MomentumSGD ( lr = <NUM_LIT> , momentum = <NUM_LIT> ) <EOL> optimizer . setup ( model ) <EOL> if args . initmodel : <EOL> print ( '<STR_LIT>' , args . initmodel ) <EOL> serializers . load_hdf5 ( args . initmodel , model ) <EOL> if args . resume : <EOL> print ( '<STR_LIT>' , args . resume ) <EOL> serializers . load_hdf5 ( args . resume , optimizer ) <EOL> data_q = queue . Queue ( maxsize = <NUM_LIT:1> ) <EOL> res_q = queue . Queue ( ) <EOL> cropwidth = <NUM_LIT> - model . insize <EOL> def read_image ( path , center = False , flip = False ) : <EOL> image = np . asarray ( Image . open ( path ) ) . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> if center : <EOL> top = left = cropwidth / <NUM_LIT:2> <EOL> else : <EOL> top = random . randint ( <NUM_LIT:0> , cropwidth - <NUM_LIT:1> ) <EOL> left = random . randint ( <NUM_LIT:0> , cropwidth - <NUM_LIT:1> ) <EOL> bottom = model . insize + top <EOL> right = model . insize + left <EOL> image = image [ : , top : bottom , left : right ] . astype ( np . float32 ) <EOL> image -= mean_image [ : , top : bottom , left : right ] <EOL> image /= <NUM_LIT:255> <EOL> if flip and random . randint ( <NUM_LIT:0> , <NUM_LIT:1> ) == <NUM_LIT:0> : <EOL> return image [ : , : , : : - <NUM_LIT:1> ] <EOL> else : <EOL> return image <EOL> def feed_data ( ) : <EOL> i = <NUM_LIT:0> <EOL> count = <NUM_LIT:0> <EOL> x_batch = np . ndarray ( <EOL> ( args . batchsize , <NUM_LIT:3> , model . insize , model . insize ) , dtype = np . float32 ) <EOL> y_batch = np . ndarray ( ( args . batchsize , ) , dtype = np . int32 ) <EOL> val_x_batch = np . ndarray ( <EOL> ( args . val_batchsize , <NUM_LIT:3> , model . insize , model . insize ) , dtype = np . float32 ) <EOL> val_y_batch = np . ndarray ( ( args . val_batchsize , ) , dtype = np . int32 ) <EOL> batch_pool = [ None ] * args . batchsize <EOL> val_batch_pool = [ None ] * args . val_batchsize <EOL> pool = multiprocessing . Pool ( args . loaderjob ) <EOL> data_q . put ( '<STR_LIT:train>' ) <EOL> for epoch in six . moves . range ( <NUM_LIT:1> , <NUM_LIT:1> + args . epoch ) : <EOL> print ( '<STR_LIT>' , epoch , file = sys . stderr ) <EOL> print ( '<STR_LIT>' , optimizer . lr , file = sys . stderr ) <EOL> perm = np . random . permutation ( len ( train_list ) ) <EOL> for idx in perm : <EOL> path , label = train_list [ idx ] <EOL> batch_pool [ i ] = pool . apply_async ( read_image , ( path , False , True ) ) <EOL> y_batch [ i ] = label <EOL> i += <NUM_LIT:1> <EOL> if i == args . batchsize : <EOL> for j , x in enumerate ( batch_pool ) : <EOL> x_batch [ j ] = x . get ( ) <EOL> data_q . put ( ( x_batch . copy ( ) , y_batch . copy ( ) ) ) <EOL> i = <NUM_LIT:0> <EOL> count += <NUM_LIT:1> <EOL> if count % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> data_q . put ( '<STR_LIT>' ) <EOL> j = <NUM_LIT:0> <EOL> for path , label in val_list : <EOL> val_batch_pool [ j ] = pool . apply_async ( <EOL> read_image , ( path , True , False ) ) <EOL> val_y_batch [ j ] = label <EOL> j += <NUM_LIT:1> <EOL> if j == args . val_batchsize : <EOL> for k , x in enumerate ( val_batch_pool ) : <EOL> val_x_batch [ k ] = x . get ( ) <EOL> data_q . put ( ( val_x_batch . copy ( ) , val_y_batch . copy ( ) ) ) <EOL> j = <NUM_LIT:0> <EOL> data_q . put ( '<STR_LIT:train>' ) <EOL> optimizer . lr *= <NUM_LIT> <EOL> pool . close ( ) <EOL> pool . join ( ) <EOL> data_q . put ( '<STR_LIT:end>' ) <EOL> def log_result ( ) : <EOL> train_count = <NUM_LIT:0> <EOL> train_cur_loss = <NUM_LIT:0> <EOL> train_cur_accuracy = <NUM_LIT:0> <EOL> begin_at = time . time ( ) <EOL> val_begin_at = None <EOL> while True : <EOL> result = res_q . get ( ) <EOL> if result == '<STR_LIT:end>' : <EOL> print ( file = sys . stderr ) <EOL> break <EOL> elif result == '<STR_LIT:train>' : <EOL> print ( file = sys . stderr ) <EOL> train = True <EOL> if val_begin_at is not None : <EOL> begin_at += time . time ( ) - val_begin_at <EOL> val_begin_at = None <EOL> continue <EOL> elif result == '<STR_LIT>' : <EOL> print ( file = sys . stderr ) <EOL> train = False <EOL> val_count = val_loss = val_accuracy = <NUM_LIT:0> <EOL> val_begin_at = time . time ( ) <EOL> continue <EOL> loss , accuracy = result <EOL> if train : <EOL> train_count += <NUM_LIT:1> <EOL> duration = time . time ( ) - begin_at <EOL> throughput = train_count * args . batchsize / duration <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( train_count , train_count * args . batchsize , <EOL> datetime . timedelta ( seconds = duration ) , throughput ) ) <EOL> train_cur_loss += loss <EOL> train_cur_accuracy += accuracy <EOL> if train_count % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> mean_loss = train_cur_loss / <NUM_LIT:1000> <EOL> mean_error = <NUM_LIT:1> - train_cur_accuracy / <NUM_LIT:1000> <EOL> print ( file = sys . stderr ) <EOL> print ( json . dumps ( { '<STR_LIT:type>' : '<STR_LIT:train>' , '<STR_LIT>' : train_count , <EOL> '<STR_LIT:error>' : mean_error , '<STR_LIT>' : mean_loss } ) ) <EOL> sys . stdout . flush ( ) <EOL> train_cur_loss = <NUM_LIT:0> <EOL> train_cur_accuracy = <NUM_LIT:0> <EOL> else : <EOL> val_count += args . val_batchsize <EOL> duration = time . time ( ) - val_begin_at <EOL> throughput = val_count / duration <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( val_count / args . val_batchsize , val_count , <EOL> datetime . timedelta ( seconds = duration ) , throughput ) ) <EOL> val_loss += loss <EOL> val_accuracy += accuracy <EOL> if val_count == <NUM_LIT> : <EOL> mean_loss = val_loss * args . val_batchsize / <NUM_LIT> <EOL> mean_error = <NUM_LIT:1> - val_accuracy * args . val_batchsize / <NUM_LIT> <EOL> print ( file = sys . stderr ) <EOL> print ( json . dumps ( { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : train_count , <EOL> '<STR_LIT:error>' : mean_error , '<STR_LIT>' : mean_loss } ) ) <EOL> sys . stdout . flush ( ) <EOL> def train_loop ( ) : <EOL> graph_generated = False <EOL> while True : <EOL> while data_q . empty ( ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> inp = data_q . get ( ) <EOL> if inp == '<STR_LIT:end>' : <EOL> res_q . put ( '<STR_LIT:end>' ) <EOL> break <EOL> elif inp == '<STR_LIT:train>' : <EOL> res_q . put ( '<STR_LIT:train>' ) <EOL> model . train = True <EOL> continue <EOL> elif inp == '<STR_LIT>' : <EOL> res_q . put ( '<STR_LIT>' ) <EOL> serializers . save_hdf5 ( args . out , model ) <EOL> serializers . save_hdf5 ( args . outstate , optimizer ) <EOL> model . train = False <EOL> continue <EOL> volatile = '<STR_LIT>' if model . train else '<STR_LIT>' <EOL> x = chainer . Variable ( xp . asarray ( inp [ <NUM_LIT:0> ] ) , volatile = volatile ) <EOL> t = chainer . Variable ( xp . asarray ( inp [ <NUM_LIT:1> ] ) , volatile = volatile ) <EOL> if model . train : <EOL> optimizer . update ( model , x , t ) <EOL> if not graph_generated : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as o : <EOL> o . write ( computational_graph . build_computational_graph ( <EOL> ( model . loss , ) ) . dump ( ) ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> graph_generated = True <EOL> else : <EOL> model ( x , t ) <EOL> res_q . put ( ( float ( model . loss . data ) , float ( model . accuracy . data ) ) ) <EOL> del x , t <EOL> feeder = threading . Thread ( target = feed_data ) <EOL> feeder . daemon = True <EOL> feeder . start ( ) <EOL> logger = threading . Thread ( target = log_result ) <EOL> logger . daemon = True <EOL> logger . start ( ) <EOL> train_loop ( ) <EOL> feeder . join ( ) <EOL> logger . join ( ) <EOL> serializers . save_hdf5 ( args . out , model ) <EOL> serializers . save_hdf5 ( args . outstate , optimizer ) </s>
