def sample(epoch, header, num_chars): with open(os.path.join(DATA_DIR, 'char_to_idx.json'), encoding="utf8") as f: char_to_idx = json.load(f) idx_to_char = {i: ch for (ch, i) in char_to_idx.items()} vocab_size = len(char_to_idx) model = build_sample_model(vocab_size) load_weights(epoch, model) model.save(os.path.join(MODEL_DIR, 'model.{}.h5'.format(epoch))) sampled = [char_to_idx[c] for c in header] print(sampled) for i in range(num_chars): batch = np.zeros((1, 1)) if sampled: batch[0, 0] = sampled[-1] else: batch[0, 0] = np.random.randint(vocab_size) result = model.predict_on_batch(batch).ravel() sample = np.random.choice(range(vocab_size), p=result) sampled.append(sample) return ''.join(idx_to_char[c] for c in sampled)
def sample(epoch, header, num_chars): with open(os.path.join(BASE_DIR, MODEL_DIR, 'char_to_idx.json'), 'r') as f: char_to_idx = json.load(f) idx_to_char = {i: ch for (ch, i) in list(char_to_idx.items())} vocab_size = len(char_to_idx) model = build_sample_model(vocab_size) load_weights(BASE_DIR, epoch, model) model.save(os.path.join(BASE_DIR, MODEL_DIR, 'model.{}.h5'.format(epoch))) sampled = [char_to_idx[c] for c in header] for c in header[:-1]: batch = np.zeros((1, 1)) batch[0, 0] = char_to_idx[c] model.predict_on_batch(batch) for i in range(num_chars): batch = np.zeros((1, 1)) if sampled: batch[0, 0] = sampled[-1] else: batch[0, 0] = np.random.randint(vocab_size) result = model.predict_on_batch(batch).ravel() sampleWord = np.random.choice(list(range(vocab_size)), p=result) sampled.append(sampleWord) return ''.join(idx_to_char[c] for c in sampled)
def train(text, epochs=100, save_freq=10, resume=False): global model, idx_to_char, vocab_size if resume: print("Attempting to resume last training...") model_dir = Path(MODEL_DIR) c2ifile = model_dir.joinpath('char_to_idx.json') with c2ifile.open('r') as f: char_to_idx = json.load(f) checkpoints = list(model_dir.glob('weights.*.h5')) if not checkpoints: raise ValueError("No checkpoints found to resume from") resume_epoch = max(int(p.name.split('.')[1]) for p in checkpoints) print("Resuming from epoch", resume_epoch) else: resume_epoch = 0 char_to_idx = {ch: i for (i, ch) in enumerate(sorted(list(set(text))))} with open(os.path.join(MODEL_DIR, 'char_to_idx.json'), 'w') as f: json.dump(char_to_idx, f) vocab_size = len(char_to_idx) idx_to_char = {i: ch for (ch, i) in list(char_to_idx.items())} model = build_model(BATCH_SIZE, SEQ_LENGTH, vocab_size) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) if resume: load_weights(resume_epoch, model) T = np.asarray([char_to_idx[c] for c in text], dtype=np.int32) log = TrainLogger('training_log.csv', resume_epoch) start = timer() for epoch in range(resume_epoch, epochs): elapsed = timer() - start start = timer() print( '\nEpoch {}/{}, previous epoch took %0.3fs or %0.3fms/step'.format( epoch + 1, epochs) % (elapsed, 1000.0 * elapsed / (T.shape[0] / BATCH_SIZE / SEQ_LENGTH))) losses, accs = [], [] for i, (X, Y) in enumerate(read_batches(T, vocab_size)): loss, acc = model.train_on_batch(X, Y) print('Batch {}: loss = {:.4f}, acc = {:.5f}\r'.format( i + 1, loss, acc), end=" ") losses.append(loss) accs.append(acc) log.add_entry(np.average(losses), np.average(accs)) print() print(sample(512)) if (epoch + 1) % save_freq == 0: save_weights(epoch + 1, model) print('Saved checkpoint to', 'weights.{}.h5'.format(epoch + 1))
def sample(epoch, header, num_lines): with open(os.path.join(MODEL_DIR, 'char_to_idx.json'), 'r') as f: char_to_idx = json.load(f) idx_to_char = {i: ch for (ch, i) in list(char_to_idx.items())} vocab_size = len(char_to_idx) newline_symbol = char_to_idx['\n'] model = build_sample_model(vocab_size) load_weights(epoch, model) sampled = [char_to_idx[c] for c in header] for c in header[:-1]: batch = np.zeros((1, 1)) batch[0, 0] = char_to_idx[c] model.predict_on_batch(batch) for i in range(num_lines): sample = -1 while not sample == newline_symbol: batch = np.zeros((1, 1)) if sampled: batch[0, 0] = sampled[-1] else: batch[0, 0] = np.random.randint(vocab_size) result = model.predict_on_batch(batch).ravel() sample = np.random.choice(list(range(vocab_size)), p=result) sampled.append(sample) return ''.join(idx_to_char[c] for c in sampled).strip()
def run(args, meta, model, callbacks, exp, id_=100, data=None): train_ds, val_ds, train_len, validation_len = prerun(args, meta, data) init_weights_path = Path(args["run_dir"], 'initial_model_weights.h5') if init_weights_path.exists(): model.load_weights(str(init_weights_path)) if not init_weights_path.exists(): hist = model.fit(train_ds, epochs=1, steps_per_epoch=1) model.save_weights(str(init_weights_path)) for i, cb in enumerate(callbacks): if type(cb) == my_callbacks.ValidationMonitor: cb.set(val_ds, validation_len, id_, exp) if type(cb) == my_callbacks.ImageLogger: cb.set_dataset(train_ds, len(args["channels"])) hist = model.fit( train_ds, epochs=args["epochs"], steps_per_epoch=int(np.ceil(train_len / args["batch_size"])), callbacks=callbacks, validation_data=val_ds, validation_steps=int(np.ceil(validation_len / args["batch_size"]))) return hist
def load(self, model_dir, load_optimizer=True, lr_scheduler=None): if not os.path.exists(model_dir): print(f'file: {model_dir} not found') load_weights(self.yolov2, self.opt.github_model) # self.init_weights(self.yolov2) return float('inf'), 0, 0 else: state_dict = torch.load(model_dir) if 'loss' in state_dict: loss = state_dict['loss'] else: loss = float('inf') if 'epoch' in state_dict: epoch = state_dict['epoch'] else: epoch = 0 if 'total_steps' in state_dict: steps = state_dict['total_steps'] else: steps = 0 if 'model' in state_dict: print(f'loading pretrained model: {model_dir}') self.yolov2.load_state_dict(state_dict['model']) if load_optimizer and 'optimizer' in state_dict: self.optimizer.load_state_dict(state_dict['optimizer']) if lr_scheduler is not None and 'lr_scheduler' in state_dict: lr_scheduler.load_state_dict(state_dict['lr_scheduler']) return loss, epoch, steps
def sample(epoch, header, num_chars): # get the character dictionary and build the character-index dict with open(os.path.join(DATA_DIR, 'char_to_idx2.json')) as f: char_to_idx2 = json.load(f) idx_to_char2 = {i: ch for (ch, i) in char_to_idx2.items()} vocab_size = len(char_to_idx2) model = build_model(1, 1, vocab_size) load_weights(epoch, model) # header implementation if needed sampled = [char_to_idx2[c] for c in header] for c in header[:-1]: batch = np.zeros((1, 1)) batch[0, 0] = char_to_idx2[c] model.predict_on_batch(batch) # sample until parameter for i in range(num_chars): batch = np.zeros((1, 1)) if sampled: batch[0, 0] = sampled[-1] else: batch[0, 0] = np.random.randint(vocab_size) result = model.predict_on_batch(batch).ravel() sample = np.random.choice(range(vocab_size), p=result) sampled.append(sample) print ''.join(idx_to_char2[c] for c in sampled)
def init_model(epoch): with open(os.path.join(MODEL_DIR, 'char_to_idx.json'), 'r') as f: char_to_idx = json.load(f) idx_to_char = { i: ch for (ch, i) in list(char_to_idx.items()) } vocab_size = len(char_to_idx) model = build_sample_model(vocab_size) load_weights(epoch, model) return model, vocab_size, idx_to_char
def train(text, epochs=100, save_freq=10, resume=False): if resume: print("Attempting to resume last training...") model_dir = Path(MODEL_DIR) c2ifile = model_dir.joinpath('char_to_idx.json') with c2ifile.open('r') as f: char_to_idx = json.load(f) checkpoints = list(model_dir.glob('weights.*.h5')) if not checkpoints: raise ValueError("No checkpoints found to resume from") resume_epoch = max(int(p.name.split('.')[1]) for p in checkpoints) print("Resuming from epoch", resume_epoch) else: resume_epoch = 0 char_to_idx = {ch: i for (i, ch) in enumerate(sorted(list(set(text))))} with open(os.path.join(MODEL_DIR, 'char_to_idx.json'), 'w') as f: json.dump(char_to_idx, f) vocab_size = len(char_to_idx) model = build_model(BATCH_SIZE, SEQ_LENGTH, vocab_size) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) if resume: load_weights(resume_epoch, model) T = np.asarray([char_to_idx[c] for c in text], dtype=np.int32) log = TrainLogger('training_log.csv', resume_epoch) for epoch in range(resume_epoch, epochs): print('\nEpoch {}/{}'.format(epoch + 1, epochs)) losses, accs = [], [] for i, (X, Y) in enumerate(read_batches(T, vocab_size)): loss, acc = model.train_on_batch(X, Y) print('Batch {}: loss = {:.4f}, acc = {:.5f}'.format(i + 1, loss, acc)) losses.append(loss) accs.append(acc) log.add_entry(np.average(losses), np.average(accs)) if (epoch + 1) % save_freq == 0: save_weights(epoch + 1, model) sample_model = build_sample_model(vocab_size) load_weights(epoch + 1, sample_model) sample_model.save('{}/SavedModel-{}'.format(MODEL_DIR, epoch + 1), save_format='tf', overwrite=True) tfjs.converters.save_keras_model(sample_model, '{}/SavedModel-{}-tfjs'.format(MODEL_DIR, epoch + 1)) print('Saved checkpoint to', 'weights.{}.h5'.format(epoch + 1))
def train(self): # self.writer.add_graph(self.trainer.yolov2.cpu(), # input_to_model=torch.rand(opt.batch_size, 3, opt.img_h, opt.img_w, device='cpu')) self.trainer.yolov2.train() loss_tmp = float('inf') if os.path.exists(opt.saved_model_path): self.logger.info(f'Use pretrained model: {opt.saved_model_path}') self.trainer.use_pretrain(opt.saved_model_path) loss_tmp = self.trainer.last_loss start_epoch = self.trainer.epoch_num total_steps = self.trainer.total_steps else: start_epoch = 0 total_steps = 0 self.logger.info('Train from stratch ...') pretrained_ckpt_path = '/home/dk/jyl/V2/COCO/model/ckpt/only_params_trained_yolo_coco' load_weights(self.trainer.yolov2, pretrained_ckpt_path, reinit_last=False) for epoch in range(start_epoch, opt.epochs): for i, (imgs, targets) in tqdm(enumerate(self.VocTrainDataLoader)): imgs, tragets = imgs.to(opt.device), targets.to(opt.device) self.trainer.train_step(imgs, tragets, epoch + 1) total_steps += 1 mean_loss = self.trainer.loss_meter.mean loss_dict = self.trainer.loss_dict self.add_train_summary(loss_dict, total_steps) if total_steps % opt.display_step == 0: message = f'Epoch[{epoch: 03}] Step[{(epoch * self.train_data_length + i + 1): 06}]] \n' \ f'mean_loss : {mean_loss:.3f} \n' \ f'xy_loss : {loss_dict["dxdy_loss"]:.3f} \n' \ f'wh_loss : {loss_dict["twth_loss"]:.3f} \n' \ f'conf_loss : {loss_dict["conf_loss"]:.3f} \n' \ f'class_loss: {loss_dict["class_loss"]:.3f} \n' \ f'obj_loss : {loss_dict["obj_loss"]:.3f} \n' \ f'noobj_loss: {loss_dict["noobj_loss"]:.3f} \n' \ f'total_loss: {loss_dict["total_loss"]:.3f} \n' \ f'current learning rate: {self.scheduler.get_lr()}' self.logger.info(message) if total_steps % opt.eval_step == 0: self.eval(epoch, i + 1) if mean_loss < loss_tmp: loss_tmp = mean_loss self.trainer.save( epoch, total_steps, mean_loss, opt.model_save_dir + f'/model_best_{opt.optimizer_type}_2.pkl') if total_steps % opt.save_step == 0: self.trainer.save( epoch, total_steps, mean_loss, opt.model_save_dir + f'/model_every_2.pkl') self.scheduler.step()
def test(lowlight_test_images_path): input_img = Input(shape=(512, 512, 3)) conv1 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(input_img) conv2 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv1) conv3 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv2) conv4 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv3) int_con1 = Concatenate(axis=-1)([conv4, conv3]) conv5 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(int_con1) int_con2 = Concatenate(axis=-1)([conv5, conv2]) conv6 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(int_con2) int_con3 = Concatenate(axis=-1)([conv6, conv1]) x_r = Conv2D(24, (3,3), strides=(1,1), activation='tanh', padding='same')(int_con3) model = Model(inputs=input_img, outputs = x_r) model.load_weights("weights/ep_2_it_200.h5") ### load image ### for test_file in glob.glob(lowlight_test_images_path + "*.jpg"): data_lowlight_path = test_file original_img = Image.open(data_lowlight_path) original_size = (np.array(original_img).shape[1], np.array(original_img).shape[0]) original_img = original_img.resize((512,512), Image.ANTIALIAS) original_img = (np.asarray(original_img)/255.0) img_lowlight = Image.open(data_lowlight_path) img_lowlight = img_lowlight.resize((512,512), Image.ANTIALIAS) img_lowlight = (np.asarray(img_lowlight)/255.0) img_lowlight = np.expand_dims(img_lowlight, 0) # img_lowlight = K.constant(img_lowlight) ### process image ### A = model.predict(img_lowlight) r1, r2, r3, r4, r5, r6, r7, r8 = A[:,:,:,:3], A[:,:,:,3:6], A[:,:,:,6:9], A[:,:,:,9:12], A[:,:,:,12:15], A[:,:,:,15:18], A[:,:,:,18:21], A[:,:,:,21:24] x = original_img + r1 * (K.pow(original_img,2)-original_img) x = x + r2 * (K.pow(x,2)-x) x = x + r3 * (K.pow(x,2)-x) enhanced_image_1 = x + r4*(K.pow(x,2)-x) x = enhanced_image_1 + r5*(K.pow(enhanced_image_1,2)-enhanced_image_1) x = x + r6*(K.pow(x,2)-x) x = x + r7*(K.pow(x,2)-x) enhance_image = x + r8*(K.pow(x,2)-x) enhance_image = tf.cast((enhance_image[0,:,:,:] * 255), dtype=np.uint8) enhance_image = Image.fromarray(enhance_image.numpy()) enhance_image = enhance_image.resize(original_size, Image.ANTIALIAS) enhance_image.save(test_file.replace(".jpg", "_rs.jpg"))
def simple_model(pretrained_weights=None, input_size=(256, 256, 1)): inputs = tf.keras.Input(input_size) conv1 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')( inputs) # 256 conv1 = tf.keras.layers.Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) conv1 = tf.keras.layers.Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) conv1 = tf.keras.layers.Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) conv1 = tf.keras.layers.Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) conv1 = tf.keras.layers.Conv2D(1, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) model = tf.keras.models.Model(inputs=inputs, outputs=conv1) model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-4), loss="mean_absolute_error", metrics=['accuracy'], run_eagerly=True) model.summary() if (pretrained_weights): model.load_weights(pretrained_weights) return model
def plot_latent_space(weightsfile): print('building model') layers = model.build_model() batch_size = 128 decoder_func = theano_funcs.create_decoder_func(layers) print('loading weights from %s' % (weightsfile)) model.load_weights([ layers['l_decoder_out'], layers['l_discriminator_out'], ], weightsfile) # regularly-spaced grid of points sampled from p(z) Z = np.mgrid[2:-2.2:-0.2, -2:2.2:0.2].reshape(2, -1).T[:, ::-1].astype(np.float32) reconstructions = [] print('generating samples') for idx in get_batch_idx(Z.shape[0], batch_size): Z_batch = Z[idx] X_batch = decoder_func(Z_batch) reconstructions.append(X_batch) X = np.vstack(reconstructions) X = X.reshape(X.shape[0], 28, 28) fig = plt.figure(1, (12., 12.)) ax1 = plt.axes(frameon=False) ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) plt.title('samples generated from latent space of autoencoder') grid = ImageGrid( fig, 111, nrows_ncols=(21, 21), share_all=True) print('plotting latent space') for i, x in enumerate(X): img = (x * 255).astype(np.uint8) grid[i].imshow(img, cmap='Greys_r') grid[i].get_xaxis().set_visible(False) grid[i].get_yaxis().set_visible(False) grid[i].set_frame_on(False) plt.savefig('latent_train_val.png', bbox_inches='tight')
def plot_latent_space(weightsfile): print('building model') layers = model.build_model() batch_size = 128 decoder_func = theano_funcs.create_decoder_func(layers) print('loading weights from %s' % (weightsfile)) model.load_weights([ layers['l_decoder_out'], layers['l_discriminator_out'], ], weightsfile) # regularly-spaced grid of points sampled from p(z) Z = np.mgrid[2:-2.2:-0.2, -2:2.2:0.2].reshape(2, -1).T[:, ::-1].astype(np.float32) reconstructions = [] print('generating samples') for idx in get_batch_idx(Z.shape[0], batch_size): Z_batch = Z[idx] X_batch = decoder_func(Z_batch) reconstructions.append(X_batch) X = np.vstack(reconstructions) X = X.reshape(X.shape[0], 28, 28) fig = plt.figure(1, (12., 12.)) ax1 = plt.axes(frameon=False) ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) plt.title('samples generated from latent space of autoencoder') grid = ImageGrid(fig, 111, nrows_ncols=(21, 21), share_all=True) print('plotting latent space') for i, x in enumerate(X): img = (x * 255).astype(np.uint8) grid[i].imshow(img, cmap='Greys_r') grid[i].get_xaxis().set_visible(False) grid[i].get_yaxis().set_visible(False) grid[i].set_frame_on(False) plt.savefig('latent_train_val.png', bbox_inches='tight')
def __init__(self, iter_no=None): weight_joint, weight_joint_group, weight_full = 1. / (3 * 17), 1. / ( 3 * 5), 1. / (3 * 1) self.weight_vec = np.array([1. / 51] * 17 + [1. / 15] * 5 + [1. / 3]) self.config = tf.ConfigProto() self.config.gpu_options.allow_growth = True self.session = tf.InteractiveSession(config=self.config) self.session.run(tf.global_variables_initializer()) if iter_no is not None: self.iter_no = iter_no print('Trying to load iter no:', self.iter_no) M.load_weights(self.iter_no, self.session, H.best_weights_path, H.best_weights_tag) else: self.iter_no = H.best_iter_no print('Trying to load iter no:', self.iter_no) M.load_weights(self.iter_no, self.session, H.best_weights_path, H.best_weights_tag)
def plot_autoencoder(weightsfile): print('building model') layers = model.build_model() batch_size = 128 print('compiling theano function') encoder_func = theano_funcs.create_encoder_func(layers) print('loading weights from %s' % (weightsfile)) model.load_weights([ layers['l_decoder_out'], layers['l_discriminator_out'], ], weightsfile) print('loading data') X_train, y_train, X_test, y_test = utils.load_mnist() train_datapoints = [] print('transforming training data') for train_idx in get_batch_idx(X_train.shape[0], batch_size): X_train_batch = X_train[train_idx] train_batch_codes = encoder_func(X_train_batch) train_datapoints.append(train_batch_codes) test_datapoints = [] print('transforming test data') for test_idx in get_batch_idx(X_test.shape[0], batch_size): X_test_batch = X_test[test_idx] test_batch_codes = encoder_func(X_test_batch) test_datapoints.append(test_batch_codes) Z_train = np.vstack(train_datapoints) Z_test = np.vstack(test_datapoints) plot(Z_train, y_train, Z_test, y_test, filename='adversarial_train_val.png', title='projected onto latent space of autoencoder')
def sample(epoch, num_chars): with open(os.path.join(DATA_DIR, 'char_to_idxi1522879818.json'), 'rb') as f: print(f) char_to_idx = json.load(f) idx_to_char = {i: ch for (ch, i) in char_to_idx.items()} vocab_size = len(char_to_idx) model = build_sample_model(vocab_size) load_weights(epoch, model) model.save( os.path.join(MODEL_DIR, 'model1522879818weights.{}.h5'.format(epoch))) li = " " treat = " " file = open(os.path.join(DATA_DIR, "result.csv"), "w") with open(os.path.join(DATA_DIR, args.seed), 'r') as myfile: for line in myfile: #li = line.rstrip() vals = line.split("#") li = vals[1] treat = vals[0] + "#" sampled = [char_to_idx[c] for c in treat] for c in treat[:-1]: batch = np.zeros((1, 1)) batch[0, 0] = char_to_idx[c] model.predict_on_batch(batch) for i in range(num_chars): batch = np.zeros((1, 1)) if sampled: batch[0, 0] = sampled[-1] else: batch[0, 0] = np.random.randint(vocab_size) result = model.predict_on_batch(batch).ravel() sample = np.random.choice(range(vocab_size), p=result) sampled.append(sample) #print(idx_to_char[c] for c in sampled) generated = ''.join(idx_to_char[c] for c in sampled) values = generated.split("#") print(generated, end='$') file.write(values[0] + ", " + values[1] + ", " + li + "\n")
def captcha_reco(): pic = flask.request.files('imagefile', '') x = prep_data.get_data(pic) model_arc = m.load_cnn.save_model_arc() cnn_model = keras.models.model_from_json(model_arc) directory = sys.path.append( os.path.join(os.path.dirname(__file__), '..', 'model')) cnn_model_final = load_weights(cnn_model, directory) result = cnn_model_final.predict({'input': x}) out = [ np.argmax(result['out'][0]), np.argmax(result['out'][1]), np.argmax(result['out'][2]), np.argmax(result['out'][3]), np.argmax(result['out'][4]) ] r = [prep_data.asc_chr(i) for i in out] result_json = json.dump(dict(r), ensure_ascii=False) return flask.Response(result_json, mimetype='application/json')
def loadModel(name): model = model_from_json(open('./model/'+ name +'_model.json').read()) model.load_weights('./model/best_validation_'+ name +'_model_weights.h5') return model
# Create a function that saves the model's weights cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath = model_name, save_weights_only=True, verbose=0, save_freq="epoch") # model.load_weights(model_name) model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, validation_data= (X_valid,Y_valid), callbacks = [cp_callback]) # Step 6: Evaluation mae, mse = model.evaluate(X_valid, Y_valid, verbose = 2) print("Evaluation, MAE: {:2.4f}, MSE: {:2.4f}".format(mae, mse)) elif sys.argv[1] == "predict": # Step 3: Loads the weights model.load_weights(model_name) my_model = tf.keras.Sequential([model]) # Step 4: Get Normalization values stats = pd.read_csv("data/wine_stats.csv", sep = ',', header = 0) # Step 5: Prepare the input AND predict input = sys.argv[2].split(",") input = np.array([float(x) for x in input]) input = loader.normalize(input, stats).values print(input) preds = my_model.predict(np.array([input])) print(preds[0])
if distributional: if alpha > 0: modelbest = file0 + "Sen2LCZ_" + str(batch_size) + "_lr_" + str( lrate) + "_urban_d" + str(alpha) + "_weights_best.hdf5" else: modelbest = file0 + "Sen2LCZ_" + str(batch_size) + "_lr_" + str( lrate) + "_urban_d" + "_weights_best.hdf5" else: modelbest = file0 + "Sen2LCZ_" + str(batch_size) + "_lr_" + str( lrate) + "_urban" + "_weights_best.hdf5" else: modelbest = file0 + "Sen2LCZ_" + str(batch_size) + "_lr_" + str( lrate) + "_weights_best.hdf5" 'load saved best model' model.load_weights(modelbest, by_name=False) # 4. test phase y_pre = model.predict(x_tst, batch_size=batch_size) y_pre = y_pre.argmax(axis=-1) + 1 y_testV = y_tst.argmax(axis=-1) + 1 # Add training data as well #y_pre_trn = model.predict(x_trn, batch_size = batch_size) #y_pre_trn = y_pre_trn.argmax(axis=-1)+1 #y_trnV = y_trn.argmax(axis=-1)+1 #y_pre = np.hstack((y_pre, y_pre_trn)) #y_testV = np.hstack((y_testV, y_trnV)) labels = [
def main(): """main function Main function... (what do you expect me to say...) Args: - none Returns: - none """ # Main function for evaluate parser = argparse.ArgumentParser( description="A testing framework for semantic segmentation.") parser.add_argument( "--net", required=True, default="unet", type=str, help= "(str) The type of net work which is either unet, deeplab or custom.") parser.add_argument("--epochs", required=False, default=500, type=int) parser.add_argument("--batch_size", required=False, default=16, type=int) parser.add_argument("--gpu_id", required=False, default="0", type=str, help="(str) The id of the gpu used when training.") parser.add_argument("--img_size", required=False, default=192, type=int, help="(int) The size of input image") parser.add_argument( "--load_weights", required=False, default=False, type=bool, help="(bool) Use old weights or not (named net_imgSize.h5)") # Parse argument args = parser.parse_args() net_type = args.net epochs = args.epochs batch_size = args.batch_size gpu_number = args.gpu_id img_size = args.img_size import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = gpu_number # Argument check if not (net_type in {"unet", "deeplab", "custom"}): raise ValueError("netType should be either unet, deeplab and custom.") # Get config Config = cfg.Config() # COCO instance print("Reading COCO ground truth...") cocoGt = COCO(Config.COCO_training_ann_path) cocoValGt = COCO(Config.COCO_validation_ann_path) print("Finished") # Get all classes classes = len(cocoGt.getCatIds()) id_to_index = dict() # There is a wired class of 0 in the feature map of type zero index_to_id = dict() # Because the id of COCO dataset starts from 92, we should project those id to index so that keras # utils can convert the segmentation map into one hot categorical encoding. for index, id in enumerate(cocoGt.getCatIds()): id_to_index[id] = index index_to_id[index] = id if net_type == "unet": model = basic_model.unet(input_size=(img_size, img_size, 3), classes=len(id_to_index)) elif net_type == "deeplab": deeplab_model = basic_model.Deeplabv3(input_shape=(img_size, img_size, 3), classes=len(id_to_index), backbone="xception") output = KL.Activation("softmax")(deeplab_model.output) model = KM.Model(deeplab_model.input, output) elif net_type == "custom": model = model.custom_model(input_shape=(img_size, img_size, 3), classes=len(id_to_index)) file_list = glob(Config.COCO_training_path + '*') val_list = glob(Config.COCO_validation_path + '*') if args.load_weights: try: model.load_weights(net_type + "_" + str(img_size) + ".h5") print("weights loaded!") except: print("weights not found!") checkpointer = KC.ModelCheckpoint(filepath=net_type + "_" + str(img_size) + ".h5", verbose=1, save_best_only=True) model.compile(optimizer=KO.Adam(), loss="categorical_crossentropy", metrics=["accuracy"]) model.fit_generator(data.generator(batch_size, file_list, (img_size, img_size), cocoGt, id_to_index, True), validation_data=data.generator(batch_size, val_list, (img_size, img_size), cocoValGt, id_to_index, False), validation_steps=10, steps_per_epoch=100, epochs=epochs, use_multiprocessing=True, workers=8, callbacks=[checkpointer]) print("Prediction start...") vfunc = np.vectorize(lambda index: index_to_id[index]) anns = [] # Convert into COCO annotation for i in trange(len(val_list)): image = val_list[i] image_id = int(image.replace(".jpg", '')[-12:]) cropping_image, padding_dims, original_size = utils.padding_and_cropping( image, (img_size, img_size)) cropping_image = preprocess_input(cropping_image, mode="torch") result = model.predict(cropping_image) result = np.argmax(result, axis=3) seg_result = utils.reverse_padding_and_cropping( result, padding_dims, original_size) seg_result = vfunc(seg_result) COCO_ann = cocostuffhelper.segmentationToCocoResult(seg_result, imgId=image_id) for ann in COCO_ann: ann["segmentation"]["counts"] = ann["segmentation"][ "counts"].decode("ascii") # json can't dump byte string anns += COCO_ann with open("result.json", "w") as file: json.dump(anns, file) # Read result file # Test for fake result #resFile = Config.fake_result # Evaluate result resFile = "result.json" cocoDt = cocoValGt.loadRes(resFile) cocoEval = COCOStuffeval(cocoValGt, cocoDt) cocoEval.evaluate() cocoEval.summarize()
import model model = model.create_model() # print(X_train) # print(y_train) callback = tf.keras.callbacks.ModelCheckpoint( 'Transformer+TimeEmbedding_avg.hdf5', monitor='val_loss', save_best_only=True, verbose=1) history = model.fit(X_train, y_train, batch_size=batch_size, epochs=10, callbacks=[callback], validation_data=(X_val, y_val)) model.load_weights('Transformer+TimeEmbedding_avg.hdf5') y = model.predict(X_test) fig = plt.figure() import datetime test_tick = test_tick.apply( lambda x: datetime.datetime.strptime(x, '%Y-%m-%d')) true_y = y * (max_return - min_return) + min_return true_ytest = y_test * (max_return - min_return) + min_return df = pd.read_csv('./input/index.csv', delimiter=',', usecols=['Date', 'Open', 'High', 'Low', 'Close', 'Volume']) real_close = df[(df.index >= last_10pct)]['Close'][49:-1].values pre_close = [] for i in range(len(real_close)):
print('RE FPR, TPR: (%.3f, %.3f)' % (fpr_re[idx3RE], tpr_re[idx3RE])) print('RE FPR, TPR: (%.3f, %.3f)' % (fpr_re[idx4RE], tpr_re[idx4RE])) print('DeepIso AUC: %.5f' % metrics.auc(fpr_nn, tpr_nn)) print('DeepIso AUC (training set): %.5f' % metrics.auc(fpr_nn_train, tpr_nn_train)) print('RelIso AUC: %.5f' % metrics.auc(fpr_re, tpr_re)) print('Now making convergence plot') auc_test = numpy.zeros(nEpochs) auc_train = numpy.zeros(nEpochs) x = numpy.linspace(1, nEpochs, nEpochs) for i in range(nEpochs): print(i) model.load_weights("weights/" + savename + "_weights_" + str(i + 1).zfill(2) + ".hdf5") prediction = model.predict([ charged_pf_features[nTrain:], photon_pf_features[nTrain:], neutralHad_pf_features[nTrain:], global_features[nTrain:] ], batch_size=nBatch) prediction_training_set = model.predict([ charged_pf_features[:nTrain], photon_pf_features[:nTrain], neutralHad_pf_features[:nTrain], global_features[:nTrain] ], batch_size=nBatch) #plt.figure() #plt.hist(prediction[(label[nTrain:]).astype(bool)], bins = 100, label = 'Signal', color = 'red') #plt.hist(prediction[numpy.logical_not(label[nTrain:])], bins = 100, label = 'Background', color = 'blue') #plt.legend(loc = 'upper left')
_, frame = vc.read() img = frame face_image = lib.align_image(img) if face_image is not None: #face_image = frame[max(0, y1):min(height, y2), max(0, x1):min(width, x2)] identity = recognize_face(face_image, input_embeddings, model) if identity is not None: #img = cv2.rectangle(frame,(x1, y1),(x2, y2),(255,255,255),2) #cv2.putText(img, str(identity), (4,30), font, 1, (255,255,255), 2) print(str(identity)) else: print("Não reconhecido") key = cv2.waitKey(100) #cv2.imshow("Face Recognizer", img) if key == 27: # exit on ESC break else: print("Nenhuma face detectada!") vc.release() cv2.destroyAllWindows() model = create_model() model.load_weights('nn4.small2.v1.h5') input_embeddings = create_input_image_embeddings(model) recognize_faces_in_cam(input_embeddings, model)
model = model.model() los = 99999 t = [] for (dirpath, dirnames, filenames) in walk(train_path): t.extend(filenames) v = [] for (dirpath, dirnames, filenames) in walk(valid_path): v.extend(filenames) t_files = len(t) v_files = len(v) try: model.load_weights("weights.h5", by_name=True) print("Loading Exiting weights........ please wait") except: print("No weights found...... model loaded with image_net weights...") pass SavingWeights = ModelCheckpoint('weights.h5', save_weights_only=True, verbose=1, save_best_only=True) for epoch in range(epochs): print("Running Epoch No==> " + str(epoch)) train_batches = generator1.data_gen(train_path, desired_size, no_channels, batch_size, shuffle) valid_batches = generator1.data_gen(valid_path, desired_size, no_channels,
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) X_val = X_val.reshape(X_val.shape[0], img_rows, img_cols, 1) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) print('X_train shape:', X_train.shape) # np_utils.to_categorical将整型标签转为onehot。 # 在这里将向量转成了矩阵 Y_train = np_utils.to_categorical(y_train, 40) Y_val = np_utils.to_categorical(y_val, 40) Y_test = np_utils.to_categorical(y_test, 40) model = model.cnn_model() # 训练模型 train.train_model(model, X_train, Y_train, X_val, Y_val, epochs) # 测试模型 score = test.test_model(model, X_test, Y_test) print(score) # 加载训练好的模型 model.load_weights('model_weights.h5') # 计算预测的类别 classes = model.predict_classes(X_test, verbose=0) # 计算正确率 test_accuracy = np.mean(np.equal(y_test, classes)) print("last accuarcy:", test_accuracy) error_num = 0 for i in range(0, 40): if y_test[i] != classes[i]: error_num += 1 print(y_test[i], '被错误分成', classes[i]) print("共有" + str(error_num) + "张图片被识别错了")
def main(): """main function Main function... (what do you expect me to say...) Args: - none Returns: - none """ # Main function for evaluate parser = argparse.ArgumentParser(description="Evaluate the model") parser.add_argument( "--net", help= "The type of net work which is either mrcnn, unet, deeplab or custom.", required=True, default="unet") parser.add_argument("--img_size", required=True, type=int, help="The size of input image") parser.add_argument("--gpu_id", required=False, default="0", type=str, help="The id of the gpu used when training.") parser.add_argument( "--weight_path", required - False, defalut=None, type=str, help= "The name of trained weights. If not specified, the model will use 'net_imgSize.h5'. " ) # Parse argument args = parser.parse_args() net_type = args.net gpu_number = args.gpu_id img_size = args.img_size weight_path = args.weight_path import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = gpu_number # Argument check if not (net_type in {"unet", "deeplab", "custom", "mrcnn"}): raise ValueError( "netType should be either unet, deeplab, mrcnn and custom.") if net_type in {"unet", "deeplab", "custom"}: # Get config Config = cfg.Config() # COCO instance print("Reading COCO ground truth...") cocoGt = COCO(Config.COCO_training_ann_path) cocoValGt = COCO(Config.COCO_validation_ann_path) print("Finished") # Get all classes classes = len(cocoGt.getCatIds()) id_to_index = dict() # There is a wired class of 0 in the feature map of type zero index_to_id = dict() # Because the id of COCO dataset starts from 92, we should project those id to index so that keras # utils can convert the segmentation map into one hot categorical encoding. for index, id in enumerate(cocoGt.getCatIds()): id_to_index[id] = index index_to_id[index] = id if net_type == "unet": model = basic_model.unet(input_size=(img_size, img_size, 3), classes=len(id_to_index)) elif net_type == "deeplab": deeplab_model = basic_model.Deeplabv3(input_shape=(img_size, img_size, 3), classes=len(id_to_index), backbone="xception") output = KL.Activation("softmax")(deeplab_model.output) model = KM.Model(deeplab_model.input, output) elif net_type == "custom": model = model.custom_model(input_shape=(img_size, img_size, 3), classes=len(id_to_index)) if net_type in {"unet", "deeplab", "custom"}: file_list = glob(Config.COCO_training_path + '*') val_list = glob(Config.COCO_validation_path + '*') if weight_path in None: model.load_weights(net_type + "_" + str(img_size) + ".h5") print("weights loaded!") else: model.load_weights(weight_path) print("weights loaded!") #model.compile(optimizer = KO.Adam(clipvalue=2.), loss="categorical_crossentropy", metrics=["accuracy"]) model.compile(optimizer=KO.Adam(), loss="categorical_crossentropy", metrics=["accuracy"]) print("Prediction start...") vfunc = np.vectorize(lambda index: index_to_id[index]) anns = [] # Convert into COCO annotation for i in trange(len(val_list)): image = val_list[i] image_id = int(image.replace(".jpg", '')[-12:]) cropping_image, padding_dims, original_size = utils.padding_and_cropping( image, (img_size, img_size)) cropping_image = preprocess_input(cropping_image, mode="torch") result = model.predict(cropping_image) result = np.argmax(result, axis=3) seg_result = utils.reverse_padding_and_cropping( result, padding_dims, original_size) seg_result = vfunc(seg_result) COCO_ann = cocostuffhelper.segmentationToCocoResult(seg_result, imgId=image_id) for ann in COCO_ann: ann["segmentation"]["counts"] = ann["segmentation"][ "counts"].decode("ascii") # json can't dump byte string anns += COCO_ann with open("result.json", "w") as file: json.dump(anns, file) # Evaluate result resFile = "result.json" cocoDt = cocoValGt.loadRes(resFile) cocoEval = COCOStuffeval(cocoValGt, cocoDt) cocoEval.evaluate() cocoEval.summarize()
if args.command == "train": model = model.MaskRCNN(config=config, model_dir=args.logs, test_flag=False) else: model = model.MaskRCNN(config=config, model_dir=args.logs, test_flag=True) if config.GPU_COUNT: model = model.cuda() if args.weights.lower() != "none": # Select weights file to load weights_path = args.weights # Load weights print("Loading weights ", weights_path) model.load_weights(weights_path) # Train or evaluate if args.command == "train": print("Training...") train(model) elif args.command == "test": print("Testing...") test(model, int(args.limit.lower()), args.save.lower(), args.bbox.lower()) else: print("'{}' is not recognized. " "Use 'train' or 'test'".format(args.command))
tf.keras.backend.set_session(sess) # モデルを作成 model = model.make(tflite=False) # 最適化を定義 optimizer = tf.keras.optimizers.Adam(lr=0.001) model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["categorical_accuracy"]) # コールバック class Callback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): "各エポック終了時に重みを保存する" model.save("weight.hdf5") cb = Callback() # 途中から学習する場合 initial_epoch = 0 if initial_epoch >= 1: model.load_weights("weight.hdf5") # 学習する model.fit_generator(g.generator(), validation_data=g.generator_test(), validation_steps=g.test_steps(), callbacks=[cb], steps_per_epoch=data.TRAIN_SIZE / data.BATCH_SIZE, epochs=50, initial_epoch=initial_epoch)
def full_model(pretrained_weights=None, input_size=(256, 256, 1)): inputs = tf.keras.Input(input_size) conv1 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')( inputs) # 256 conv1 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool1) # 128 conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool2) # 64 conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool3) # 32 conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv4) drop4 = Dropout(0.5)(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(drop4) conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool4) # 16 conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv5) drop5 = Dropout(0.5)(conv5) up6 = Conv2D(512, 2, activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(drop5)) merge6 = concatenate([drop4, up6], axis=3) conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6) conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv6) up7 = Conv2D(256, 2, activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(conv6)) merge7 = concatenate([conv3, up7], axis=3) conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge7) conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7) up8 = Conv2D(128, 2, activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(conv7)) merge8 = concatenate([conv2, up8], axis=3) conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge8) conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv8) up9 = Conv2D(64, 2, activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(conv8)) merge9 = concatenate([conv1, up9], axis=3) conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge9) conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9) conv9 = Conv2D(2, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9) # conv10 = Conv2D(1, 1, activation = 'relu')(conv9) # conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv10) conv10 = Conv2D(1, 1, activation='sigmoid')(conv9) model = tf.keras.models.Model(inputs=inputs, outputs=conv10) model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-4), loss="mean_absolute_error", metrics=['accuracy'], run_eagerly=True) model.summary() if (pretrained_weights): model.load_weights(pretrained_weights) return model
# load nlp model # nlp_model = NLPModel() out_idx = nlp_model.load_output_term_index() print(repr(out_idx)) dictionary, max_len = nlp_model.load_input_dict() print repr(dictionary) model = nlp_model.create_keras_model() model.load_weights(KERAS_WEIGHTS_FN) # # main # db = LogicDB(session) engine = PrologEngine(db) prolog_builtins.register_builtins(engine) parser = PrologParser() while True: line = raw_input('nlp> ')
import glob import six.moves.cPickle from keras.models import model_from_json import model tokeniser = six.moves.cPickle.load(open("tokeniser.pkl", 'rb')) lastModel = sorted(glob.glob('model-*.h5'))[-1] model = model_from_json(open("model.json").read()) model.load_weights(lastModel) dictionarySize = int(open('model-dictionary-size.dat').read()) print(model.query(model, tokeniser, dictionarySize, "It is bad")) print(model.query(model, tokeniser, dictionarySize, "It is good"))