def test_value_manipulation(self): val = np.random.random((4, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) # get_value valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # set_value val = np.random.random((4, 2)) KTH.set_value(xth, val) KTF.set_value(xtf, val) valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # count_params assert KTH.count_params(xth) == KTF.count_params(xtf) # print_tensor check_single_tensor_operation('print_tensor', ()) check_single_tensor_operation('print_tensor', (2, )) check_single_tensor_operation('print_tensor', (4, 3)) check_single_tensor_operation('print_tensor', (1, 2, 3)) val = np.random.random((3, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) assert KTH.get_variable_shape(xth) == KTF.get_variable_shape(xtf)
def test_value_manipulation(self): val = np.random.random((4, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) # get_value valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # set_value val = np.random.random((4, 2)) KTH.set_value(xth, val) KTF.set_value(xtf, val) valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # count_params assert KTH.count_params(xth) == KTF.count_params(xtf) # print_tensor check_single_tensor_operation('print_tensor', ()) check_single_tensor_operation('print_tensor', (2,)) check_single_tensor_operation('print_tensor', (4, 3)) check_single_tensor_operation('print_tensor', (1, 2, 3)) val = np.random.random((3, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) assert KTH.get_variable_shape(xth) == KTF.get_variable_shape(xtf)
def reset_states(self, states_value=None): if len(self.states) == 0: return if not self.stateful: raise AttributeError('Layer must be stateful.') if not hasattr(self, 'states') or self.states[0] is None: state_shapes = list(map(K.int_shape, self.model.input[1:])) self.states = list(map(K.zeros, state_shapes)) if states_value is not None: if type(states_value) not in (list, tuple): states_value = [states_value] * len(self.states) assert len(states_value) == len( self.states), 'Your RNN has ' + str(len( self.states)) + ' states, but was provided ' + str( len(states_value)) + ' state values.' if 'numpy' not in type(states_value[0]): states_value = list(map(np.array, states_value)) if states_value[0].shape == tuple(): for state, val in zip(self.states, states_value): K.set_value(state, K.get_value(state) * 0. + val) else: for state, val in zip(self.states, states_value): K.set_value(state, val) else: if self.state_initializer: for state, init in zip(self.states, self.state_initializer): if isinstance(init, initializers.Zeros): K.set_value(state, 0 * K.get_value(state)) else: K.set_value(state, K.eval(init(K.get_value(state).shape))) else: for state in self.states: K.set_value(state, 0 * K.get_value(state))
def on_epoch_end(self, epoch, logs={}): if epoch > self.klstart: #grows linearly towards one new_weight = min( K.get_value(self.kl_weight) + (1 / self.kl_annealtime), 1.) print(new_weight) K.set_value(self.kl_weight, new_weight) print("Current KL Weight is " + str(K.get_value(self.kl_weight)))
def on_epoch_end(self, epoch, logs={}): K.set_value(self.w_cla, (-self.sigmoid[epoch] + 1) * self.scale_c[epoch]) K.set_value(self.w_dec, (self.sigmoid[epoch]) * self.scale_d[epoch]) self.w_decs.append(K.get_value(self.w_dec)) self.w_clas.append(K.get_value(self.w_cla))
def test_random_normal(self): mean = 0.0 std = 1.0 rand = KTF.get_value(KTF.random_normal((1000, 1000), mean=mean, std=std)) assert rand.shape == (1000, 1000) assert np.abs(np.mean(rand) - mean) < 0.01 assert np.abs(np.std(rand) - std) < 0.01 rand = KTF.get_value(KTF.random_normal((1000, 1000), mean=mean, std=std)) assert rand.shape == (1000, 1000) assert np.abs(np.mean(rand) - mean) < 0.01 assert np.abs(np.std(rand) - std) < 0.01
def test_random_uniform(self): mean = 0. std = 1. rand = KTF.get_value(KTF.random_normal((1000, 1000), mean=mean, std=std)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - mean) < 0.01) assert(np.abs(np.std(rand) - std) < 0.01) rand = KTF.get_value(KTF.random_normal((1000, 1000), mean=mean, std=std)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - mean) < 0.01) assert(np.abs(np.std(rand) - std) < 0.01)
def train(): global args args = parser.parse_args() print(args) train_videos = PennAction(frames_path='data/PennAction/train/frames/', labels_path='data/PennAction/train/labels', batch_size=args.batch_size, num_frames_sampled=args.num_frames_sampled) valid_videos = PennAction(frames_path='data/PennAction/validation/frames', labels_path='data/PennAction/validation/labels', batch_size=args.batch_size, num_frames_sampled=args.num_frames_sampled, shuffle=False) reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=np.sqrt(0.1), patience=5, verbose=1) save_best = ModelCheckpoint(args.filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks = [save_best, reduce_lr] if os.path.exists(args.filepath): model = load_model(args.filepath) else: model = VGG19_GRU(frames_input_shape=(args.num_frames_sampled, 224, 224, 3), poses_input_shape=(args.num_frames_sampled, 26), classes=len(train_videos.labels)) model.compile(optimizer=Adam(lr=args.train_lr, decay=1e-5), loss='categorical_crossentropy', metrics=['acc']) print('Train the GRU component only') model.fit_generator(generator=train_videos, epochs=args.epochs, callbacks=callbacks, workers=args.num_workers, validation_data=valid_videos) # Clear session to avoid exhausting GPU's memory # Reload the model architecture and weights, unfreeze the last 2 convolutional layers in VGG19's block 5 # Recompile the model with halved learning rate (from the last checkpoint) K.clear_session() model = load_model(args.filepath) model.layers[-9].trainable = True model.layers[-10].trainable = True model.compile(optimizer=Adam(lr=K.get_value(model.optimizer.lr) * 0.5, decay=1e-5), loss='categorical_crossentropy', metrics=['acc']) print('Fine-tune top 2 convolutional layers of VGG19') model.fit_generator(generator=train_videos, epochs=args.epochs, callbacks=callbacks, workers=args.num_workers, validation_data=valid_videos)
def test_function(self): val = np.random.random((4, 2)) input_val = np.random.random((4, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) yth = KTH.placeholder(ndim=2) ytf = KTF.placeholder(ndim=2) exp_th = KTH.square(xth) + yth exp_tf = KTF.square(xtf) + ytf update_th = xth * 2 update_tf = xtf * 2 fth = KTH.function([yth], [exp_th], updates=[(xth, update_th)]) ftf = KTF.function([ytf], [exp_tf], updates=[(xtf, update_tf)]) function_outputs_th = fth([input_val])[0] function_outputs_tf = ftf([input_val])[0] assert function_outputs_th.shape == function_outputs_tf.shape assert_allclose(function_outputs_th, function_outputs_tf, atol=1e-05) new_val_th = KTH.get_value(xth) new_val_tf = KTF.get_value(xtf) assert new_val_th.shape == new_val_tf.shape assert_allclose(new_val_th, new_val_tf, atol=1e-05)
def on_batch_end(self, batch, logs={}): tmp = [time.asctime(),logs.get('loss').tolist(), ktf.get_value(self.model.optimizer.lr).tolist()] self.f_batch.write(','+json.dumps(tmp)) self.f_batch_local.write(','+json.dumps(tmp)) flush_file(self.f_batch) flush_file(self.f_batch_local)
def on_epoch_end(self, batch, logs={}): self.losses.append([time.asctime(),logs.get('loss'), logs.get('val_loss'), ktf.get_value(self.model.optimizer.lr).tolist()]) with open("{0}_loss.json".format(self.root), 'w') as f: json.dump([self.extra, self.losses], f) with open("/home/aasensio/{0}_loss.json".format(platform.node()), 'w') as f: json.dump([self.extra, self.losses], f)
def on_epoch_end(self, epoch, logs={}): if (self.current <= (self.l_period / 2)): self.current = self.current + 1 K.set_value(self.w_cla, 0) K.set_value(self.w_dec, 1) else: self.current = self.current + 1 K.set_value(self.w_cla, 1) K.set_value(self.w_dec, 0) if (self.current == self.l_period): self.current = 0 self.w_decs.append(K.get_value(self.w_dec)) self.w_clas.append(K.get_value(self.w_cla))
def on_epoch_end(self, epoch, logs={}): if epoch > self.klstart: #grows linearly towards one new_weight = min( K.get_value(self.kl_weight) + (self.max_kl_weight / self.kl_annealtime), self.max_kl_weight) #print(new_weight) K.set_value(self.kl_weight, new_weight)
def predict(img_path, base_model, thresholding=160): t = Timer() img = Image.open(img_path) im = img.convert('L') scale = im.size[1] * 1.0 / 32 w = im.size[0] / scale w = int(w) print('w:', w) im = im.resize((w, 32), Image.ANTIALIAS) img = np.array(im).astype(np.float32) / 255.0 - 0.5 X = img.reshape((32, w, 1)) X = np.array([X]) t.tic() y_pred = base_model.predict(X) t.toc() print("times,", t.diff) argmax = np.argmax(y_pred, axis=2)[0] y_pred = y_pred[:, :, :] out = K.get_value(K.ctc_decode(y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])[:, :] out = u''.join([id_to_char[x] for x in out[0]]) return out, im
def on_batch_end(self, batch, logs={}): tmp = [time.asctime(),logs.get('loss').tolist(), ktf.get_value(self.model.optimizer.lr).tolist()] # self.f_batch.write(','+json.dumps(tmp)) self.f_batch_local.write(','+json.dumps(tmp)) # flush_file(self.f_batch) flush_file(self.f_batch_local)
def predict_model(model, input_): pred_ = model.predict(input_) shape = pred_[:, :, :].shape ctc_decode = K.ctc_decode(pred_[:, :, :], input_length=np.ones(shape[0]) * shape[1])[0][0] output_ = K.get_value(ctc_decode) # return output_[:, :ocr.MAX_CAPTCHA] return output_
def on_epoch_end(self, epoch, logs={}): tau = np.mod(epoch, int(self.T / self.M)) * 1.0 / int(self.T / self.M) if tau >= self.R: new_weight = 1.0 / 5000 #capped at 0.0002, because posterior collapose happens! else: new_weight = tau / 5000 K.set_value(self.kl_weight, new_weight) print("Current KL Weight is " + str(K.get_value(self.kl_weight)))
def test_model(model, X_test, Y_test): print("X_test:", X_test.shape) print("Y_test:", Y_test.shape) y_pred = model.predict(X_test) shape = y_pred[:, :, :].shape ctc_decode = K.ctc_decode(y_pred[:, :, :], input_length=np.ones(shape[0]) * shape[1])[0][0] out = K.get_value(ctc_decode)[:, :MAX_CAPTCHA] accur = np.sum(abs(out - Y_test), axis=1) accur_score = len(accur[accur == 0]) * 1.0 / len(accur) print("accur_score:", accur_score)
def test_value_manipulation(self): val = np.random.random((4, 2)) xth = KTH.variable(val) xtf = KTF.variable(val) # get_value valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # set_value val = np.random.random((4, 2)) KTH.set_value(xth, val) KTF.set_value(xtf, val) valth = KTH.get_value(xth) valtf = KTF.get_value(xtf) assert valtf.shape == valth.shape assert_allclose(valth, valtf, atol=1e-05) # count_params assert KTH.count_params(xth) == KTF.count_params(xtf)
def predict(img_path, base_model, thresholding=160): """ thresholding 输入范围 0 - 255 默认为160 0 : 采用自动阈值 > 0 : 采用人工设置的阈值 """ if thresholding > 255: thresholding = 255 if thresholding < 0: thresholding = 0 t = Timer() img = Image.open(img_path) im = img.convert('L') scale = im.size[1] * 1.0 / 64 w = im.size[0] / scale w = int(w) # print('w:',w) im = im.resize((160, 32), Image.ANTIALIAS) img = np.array(im) h, w = img.shape if thresholding == 0: img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 5) else: for i in range(h): for j in range(w): if img[i, j] > thresholding: img[i, j] = 255 else: img[i, j] = 0 img = np.array(img) img = img.astype(np.float32) / 255.0 - 0.5 X = img.reshape((32, 160, 1)) X = np.array([X]) t.tic() y_pred = base_model.predict(X) t.toc() # print("times,",t.diff) argmax = np.argmax(y_pred, axis=2)[0] y_pred = y_pred[:, :, :] out = K.get_value(K.ctc_decode(y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])[:, :] out = u''.join([id_to_char[x] for x in out[0]]) return out, im
def predict(img_path, base_model, thresholding=160): if thresholding > 255: thresholding = 255 if thresholding < 0: thresholding = 0 t = Timer() img = Image.open(img_path).convert('L') w, h = img.size rate = w / h img = img.resize((int(rate * 32), 32), Image.ANTIALIAS) img = np.array(img) # if thresholding == 0: # img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 5) # for i in range(32): # for j in range(int(rate * 32)): # if img[i,j] > 160: # img[i,j] = 255 # else: # img[i,j] = 0 img = np.array(img, 'f') / 255.0 - 0.5 t_img = np.zeros((32, 512)) t_img[:, :int(rate * 32)] = img X = np.array([t_img]) X = X.reshape((1, 32, 512, 1)) t.tic() y_pred = base_model.predict(X) t.toc() print("times,", t.diff) argmax = np.argmax(y_pred, axis=2)[0] y_pred = y_pred[:, :, :] out = K.get_value( K.ctc_decode( y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])[:, :] out = u''.join([id_to_char[x] for x in out[0]]) return out, t_img
def __text_recognition(self): self._rec_results = [] for box in self._boxes: test_img = self._image[box[1]:box[7], box[0]:box[6]] test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY) scale = test_img.shape[0] * 1.0 / 32 w = test_img.shape[1] / scale w = int(w) test_img = cv2.resize(test_img, (w, 32)) test_img = np.array(test_img).astype(np.float32) / 255.0 - 0.5 X = test_img.reshape((32, w, 1)) X = np.array([X]) y_pred = self._ocr_model.predict(X) y_pred = y_pred[:, :, :] word = K.get_value( K.ctc_decode( y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])[:, :] words = u''.join([id_to_char[x] for x in word[0]]) self._rec_results.append(words)
def predict(img_path, base_model): img = Image.open(img_path).convert('L') w, h = img.size rate = w / h img = img.resize((int(rate * 32), 32), Image.ANTIALIAS) img = np.array(img).astype(np.float32) / 255.0 - 0.5 x = img.reshape(1, 32, int(rate * 32), 1) y_pred = base_model.predict(x) print(np.argmax(y_pred, axis=2)[0]) y_pred = y_pred[:, :, :] print( type( K.ctc_decode( y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])) out = K.get_value( K.ctc_decode( y_pred, input_length=np.ones(y_pred.shape[0]) * y_pred.shape[1], )[0][0])[:, :] out = u''.join([id_to_char[x] for x in out[0]]) return out, img
def on_epoch_begin(self, epoch, logs={}): print("Learning rate:", K.get_value(model.optimizer.lr))
def run_layer(keras_layer, x): return get_value(keras_layer.call(_to_tensor(np.expand_dims(x, 0), FLOAT_DTYPE))).squeeze()
def train(self, batch_size=16, train_Steps=100, val_Steps=5, nEpochs=100): gen_S_train = DataGenerator(self.n_classes, self.img_res, self.st_path, batch_size) gen_S_val = DataGenerator(self.n_classes, self.img_res, self.sv_path, batch_size) gen_T_train = DataGenerator(self.n_classes, self.img_res, self.tt_path, batch_size) gen_T_val = DataGenerator(self.n_classes, self.img_res, self.tv_path, batch_size) gen_S_train.on_epoch_end() gen_T_train.on_epoch_end() prev = 0 for epoch in range(nEpochs): # setting parameters K.set_value(self.gradInv.hp_lambda, self._hp_scheduler(epoch, nEpochs)) lr = self._lr_scheduler(epoch, nEpochs) self.compile(self.classifier, lr, name='classifier') self.compile(self.discriminator, lr*0.1, name='discrimimator') # train res_S_train = np.array([0., 0.]) res_d_train = np.asarray([0., 0.]) for batch in tqdm.tqdm(range(train_Steps)): xSt, ySt = gen_S_train.next() lSt = self.classifier.train_on_batch(xSt, ySt) res_S_train += np.asarray(lSt) xTt, _ = gen_T_train.next() dSt = np.zeros((batch_size)) dTt = np.ones((batch_size)) ldt = self.discriminator.train_on_batch(np.concatenate((xSt, xTt)), np.concatenate((dSt, dTt))) res_d_train += np.asarray(ldt) los_S_train, acc_S_train = res_S_train / train_Steps los_d_train, acc_d_train = res_d_train / train_Steps # valid res_S_val = np.array([0., 0.]) res_T_val = np.asarray([0., 0.]) res_d_val = np.asarray([0., 0.]) for batch in range(val_Steps): xSv, ySv = gen_S_val.next() lSv = self.classifier.test_on_batch(xSv, ySv) res_S_val += np.asarray(lSv) xTv, yTv = gen_T_val.next() lTv = self.classifier.test_on_batch(xTv, yTv) res_T_val += np.asarray(lTv) dSv = np.zeros((batch_size)) dTv = np.ones((batch_size)) ld = self.discriminator.test_on_batch(np.concatenate((xSv, xTv)), np.concatenate((dSv, dTv))) res_d_val += np.asarray(ld) los_S_val, acc_S_val = res_S_val / val_Steps los_T_val, acc_T_val = res_T_val / val_Steps los_d_val, acc_d_val = res_d_val / val_Steps gen_S_val.on_epoch_end() gen_T_val.on_epoch_end() val_lr_lambda = K.get_value(self.classifier.optimizer.lr) val_hp_lambda = K.get_value(self.gradInv.hp_lambda) print() print("[Epoch %d, lr=%.1e, lambda=%.1e]" % (epoch, val_lr_lambda, val_hp_lambda)) print("<Train> [Dom loss: %.2f, acc: %3d%%] [Cls(S) loss: %.2f, acc: %3d%%]" % (los_d_train, acc_d_train*100., los_S_train, acc_S_train*100.,)) print("<Val> [Dom loss: %.2f, acc: %3d%%] [Cls(S) loss: %.2f, acc: %3d%%] [Cls(T) loss: %.2f, acc: %3d%%]" % (los_d_val, acc_d_val*100., los_S_val, acc_S_val*100., los_T_val, acc_T_val*100.,)) # save weights when Target loss is the minimum if acc_T_val >= prev: self.classifier.save_weights(os.path.join(self.output_name, 'dann_acc-{:.2f}_loss-{:.2f}.hdf5'.format(acc_T_val, los_T_val))) prev = acc_T_val # draw graph outlines = [] outlines.append(epoch) outlines.append(val_lr_lambda) outlines.append(val_hp_lambda) outlines.append(los_d_train) outlines.append(acc_d_train*100.) outlines.append(los_S_train) outlines.append(acc_S_train*100.) outlines.append(los_d_val) outlines.append(acc_d_val*100.) outlines.append(los_S_val) outlines.append(acc_S_val*100.) outlines.append(los_T_val) outlines.append(acc_T_val*100.) self.output_file.write(",".join([str(x) for x in outlines])+"\n") self.output_file.flush() d = pd.read_csv(self.output_name+"/progress.csv") if d.shape[0] == 1: continue d = d.interpolate() p = d.plot(x="epoch", y=["acc_d_v", "acc_c_v_s", "acc_c_v_t"]) fig = p.get_figure() fig.savefig(self.output_name+"/graph.png") plt.close()
def train(): global args args = parser.parse_args() print(args) train_videos = MyVideos(frames_path='data/MyVideos/train/frames/', poses_path='data/MyVideos/train/poses', batch_size=args.batch_size, num_frames_sampled=args.num_frames_sampled) valid_videos = MyVideos(frames_path='data/MyVideos/validation/frames', poses_path='data/MyVideos/validation/poses', batch_size=args.batch_size, num_frames_sampled=args.num_frames_sampled, shuffle=False) reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=np.sqrt(0.1), min_lr=1e-6, patience=5, verbose=1) save_best = ModelCheckpoint(args.filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks = [save_best, reduce_lr] if os.path.exists(args.filepath): model = load_model(args.filepath) else: pretrained_model = load_model(args.pretrained) model = VGG19_GRU(frames_input_shape=(args.num_frames_sampled, 224, 224, 3), poses_input_shape=(args.num_frames_sampled, 26), classes=len(train_videos.labels)) for i, layer in enumerate(model.layers[:-3]): layer.set_weights(pretrained_model.layers[i].get_weights()) model.compile(optimizer=Adam(lr=args.train_lr, decay=1e-6), loss='categorical_crossentropy', metrics=['acc']) # hacky trick to avoid exhausting GPU's memory model.save(args.filepath) K.clear_session() model = load_model(args.filepath) print('Train the GRU component only') model.fit_generator(generator=train_videos, epochs=args.epochs, callbacks=callbacks, workers=args.num_workers, validation_data=valid_videos) model = load_model(args.filepath) model.layers[-9].trainable = True model.layers[-10].trainable = True model.compile(optimizer=Adam(lr=K.get_value(model.optimizer.lr) * 0.5, decay=1e-6), loss='categorical_crossentropy', metrics=['acc']) print('Fine-tune top 2 convolutional layers of VGG19') model.fit_generator(generator=train_videos, epochs=args.epochs, callbacks=callbacks, workers=args.num_workers, validation_data=valid_videos)
def on_batch_end(self, batch, logs=None): self.global_step = self.global_step + 1 lr = K.get_value(self.model.optimizer.lr) self.learning_rates.append(lr)
def on_epoch_end(self, epoch, logs={}): new_beta = max(K.get_value(self.beta) * self.decay, self.min_beta) K.set_value(self.beta, new_beta) print("Current beta is " + str(K.get_value(self.beta)))