def test_prednet(initmodel, sequence_list, size, channels, gpu, output_dir="result", skip_save_frames=0, extension_start=0, extension_duration=0, offset=[0, 0], reset_each=False, verbose=1, reset_at=-1, input_len=-1, c_dim=3): #Create Model prednet = net.PredNet(size[0], size[1], channels) model = L.Classifier(prednet, lossfun=mean_squared_error) model.compute_accuracy = False # optimizer = optimizers.Adam() # optimizer.setup(model) if gpu >= 0: cuda.check_cuda_available() xp = cuda.cupy cuda.get_device(gpu).use() model.to_gpu() print('Running on GPU') else: xp = np print('Running on CPU') # Init/Resume serializers.load_npz(initmodel, model) logf = open('test_log.txt', 'w') step = 0 if verbose == 1: print("sequence_list ", sequence_list) for image_list in sequence_list: step = test_image_list(prednet, image_list, model, output_dir, channels, size, offset, gpu, logf, skip_save_frames, extension_start, extension_duration, reset_each, step, verbose, reset_at, input_len, c_dim)
def train_prednet(initmodel, sequence_list, gpu, size, channels, offset, resume, bprop, output_dir="result", period=1000000, save=10000, verbose=1, c=3): if not os.path.exists('models'): os.makedirs('models') if not os.path.exists('images'): os.makedirs('images') #Create Model prednet = net.PredNet(size[0], size[1], channels) model = L.Classifier(prednet, lossfun=mean_squared_error) model.compute_accuracy = False optimizer = optimizers.Adam() optimizer.setup(model) if gpu >= 0: cuda.check_cuda_available() xp = cuda.cupy cuda.get_device(gpu).use() model.to_gpu() print('Running on GPU') else: xp = np print('Running on CPU') # Init/Resume if initmodel: print('Load model from', initmodel) serializers.load_npz(initmodel, model) if resume: print('Load optimizer state from', resume) serializers.load_npz(resume, optimizer) train_image_sequences(sequence_list, prednet, model, optimizer, channels, size, offset, gpu, period, save, bprop, verbose, c)
args.size = args.size.split(',') for i in range(len(args.size)): args.size[i] = int(args.size[i]) args.channels = args.channels.split(',') for i in range(len(args.channels)): args.channels[i] = int(args.channels[i]) args.offset = args.offset.split(',') for i in range(len(args.offset)): args.offset[i] = int(args.offset[i]) if args.gpu >= 0: cuda.check_cuda_available() xp = cuda.cupy if args.gpu >= 0 else np #Create Model prednet = net.PredNet(args.size[0], args.size[1], args.channels) model = L.Classifier(prednet, lossfun=mean_squared_error) model.compute_accuracy = False optimizer = optimizers.Adam() optimizer.setup(model) if args.gpu >= 0: cuda.get_device(args.gpu).use() model.to_gpu() print('Running on a GPU') else: print('Running on a CPU') # Init/Resume if args.initmodel: print('Load model from', args.initmodel)
def main(self): image_list = self.load_image_list(self.args.filename) self.prednet = net.PredNet(self.in_width, self.in_height, (self.in_channel, 48, 96, 192), (self.in_channel, 48, 96, 192)) self.model = L.Classifier(self.prednet, lossfun=mean_squared_error) self.model.compute_accuracy = False self.optimizer = optimizers.Adam() self.optimizer.setup(self.model) if self.args.test: self.prednet.reset_state() self.model.zerograds() loss = 0 batch_size = 1 x_batch = np.ndarray( (batch_size, self.in_channel, self.in_height, self.in_width), dtype=np.float32) y_batch = np.ndarray( (batch_size, self.in_channel, self.in_height, self.in_width), dtype=np.float32) for i in range(0, len(image_list)): print('frameNo:' + str(i)) x_batch[0] = self.read_image(image_list[i]) loss += self.model(chainer.Variable(self.xp.asarray(x_batch)), chainer.Variable(self.xp.asarray(y_batch))) loss.unchain_backward() loss = 0 self.model.to_cpu() self.write_image(x_batch[0].copy(), 'out/' + str(i) + 'x.jpg') self.write_image(self.model.y.data[0].copy(), 'out/' + str(i) + 'y.jpg') self.model.to_gpu() self.model.to_cpu() x_batch[0] = self.model.y.data[0].copy() self.model.to_gpu() for i in range(len(image_list), len(image_list) + 100): print('extended frameNo:' + str(i)) loss += self.model(chainer.Variable(self.xp.asarray(x_batch)), chainer.Variable(self.xp.asarray(y_batch))) loss.unchain_backward() loss = 0 self.model.to_cpu() self.write_image(self.model.y.data[0].copy(), 'out/' + str(i) + 'y.jpg') x_batch[0] = self.model.y.data[0].copy() self.model.to_gpu() else: for num in range(0, 10000): bprop_len = 20 self.prednet.reset_state() self.model.zerograds() loss = 0 batch_size = 1 x_batch = np.ndarray((batch_size, self.in_channel, self.in_height, self.in_width), dtype=np.float32) y_batch = np.ndarray((batch_size, self.in_channel, self.in_height, self.in_width), dtype=np.float32) x_batch[0] = self.read_image(image_list[0]) for i in range(1, len(image_list)): y_batch[0] = self.read_image(image_list[i]) loss += self.model( chainer.Variable(self.xp.asarray(x_batch)), chainer.Variable(self.xp.asarray(y_batch))) print('frameNo:' + str(i)) if (i + 1) % bprop_len == 0: print('i = {}'.format(i)) self.model.zerograds() loss.backward() loss.unchain_backward() loss = 0 self.optimizer.update() self.model.to_cpu() # self.write_image(x_batch[0].copy(), 'out/' + str(num) + '_' + str(i) + 'a.jpg') self.write_image(x_batch[0].copy(), 'out/{}_{}a.jpg'.format(num, i)) # self.write_image(self.model.y.data[0].copy(), 'out/' + str(num) + '_' + str(i) + 'b.jpg') self.write_image(self.model.y.data[0].copy(), 'out/{}_{}b.jpg'.format(num, i)) # self.write_image(y_batch[0].copy(), 'out/' + str(num) + '_' + str(i) + 'c.jpg') self.write_image(y_batch[0].copy(), 'out/{}_{}c.jpg'.format(num, i)) self.model.to_gpu() print('loss:' + str(float(self.model.loss.data))) if i == 1 and (num % 10) == 0: print('save the model') serializers.save_npz('out/' + str(num) + '.model', self.model) print('save the optimizer') serializers.save_npz('out/' + str(num) + '.state', self.optimizer) x_batch[0] = y_batch[0]
parser.add_argument('--resume', default='', help='Resume the optimization from snapshot') parser.add_argument('--test', dest='test', action='store_true') parser.set_defaults(test=False) args = parser.parse_args() if args.gpu >= 0: cuda.check_cuda_available() xp = cuda.cupy if args.gpu >= 0 else np #Create Model inWidth = 160 inHeight = 128 inChannel = 3 prednet = net.PredNet(inWidth, inHeight, (inChannel, 48, 96, 192), (inChannel, 48, 96, 192)) model = L.Classifier(prednet, lossfun=mean_squared_error) model.compute_accuracy = False optimizer = optimizers.Adam() optimizer.setup(model) if args.gpu >= 0: cuda.get_device(args.gpu).use() model.to_gpu() # Init/Resume if args.initmodel: print('Load model from', args.initmodel) serializers.load_npz(args.initmodel, model) if args.resume: print('Load optimizer state from', args.resume)