def main(args: argparse.Namespace) -> int: train_set, val_set = split_dataset(dataset_name=args.annotations_filename, val_split=args.val_split, input_path=args.input_folder, output_path=args.output_folder) data_augmentation(args.data_aug_params, train_set, data_folder=args.output_folder + 'train_set/', aug_steps=args.aug_steps) export_dataset(train_set, args.format, args.output_folder) return 0
def backward(train_data, train_num): with tf.Graph().as_default() as g: with tf.name_scope('input'): x = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, IMG_SIZE[0], IMG_SIZE[1], IMG_CHANNEL]) y_ = tf.placeholder(dtype=tf.float32, shape=[BATCH_SIZE, IMG_SIZE[0], IMG_SIZE[1], IMG_CHANNEL]) # forward y = model.forward(x) # learning rate global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, train_num // BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) # loss function with tf.name_scope('loss'): loss = loss_func(y_,y) # Optimizer with tf.name_scope('train'): # Adam optimizer = tf.train.AdamOptimizer(learning_rate) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step=global_step) # Save model saver = tf.train.Saver(max_to_keep=30) epoch = 0 config = tf.ConfigProto(log_device_placement=True) with tf.Session(config=config) as sess: tf.global_variables_initializer().run() ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) epoch = int(ckpt.model_checkpoint_path.split('/')[-1].split('_')[-1].split('-')[-2]) while epoch < MAX_EPOCH: max_step = train_num // BATCH_SIZE listtmp = np.random.permutation(train_num) j = 0 for i in range(max_step): file = open("loss.txt", 'a') ind = listtmp[j:j + BATCH_SIZE] j = j + BATCH_SIZE xs = train_data[ind, :, :, :] mode = np.random.permutation(8) xs = DA.data_augmentation(xs,mode[0]) _, loss_v, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: xs}) file.write("Epoch: %d Step is: %d After [ %d / %d ] training, the batch loss is %g.\n" % ( epoch + 1, step, i + 1, max_step, loss_v)) file.close() # print("Epoch: %d After [ %d / %d ] training, the batch loss is %g." % (epoch + 1, i + 1, max_step, loss_v)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME + '_epoch_' + str(epoch + 1)), global_step=global_step) epoch += 1
def proc(name, k): im = cv2.imread(name) imre = create_fixed_image_shape(bbox(im), temp_shape, random_fill=False, mode='fit') imgs = data_augmentation(imre, frame_size=out_shape) for i in range(len(imgs)): img = imgs[i] imgs.append(img[:, ::-1, :]) for i, img in enumerate(imgs): new_name = name.replace(in_folder, out_folder) new_name = new_name.replace('.jpeg', '_%d.jpeg' % i) cv2.imwrite(new_name, img) if k % 10 == 0: print "Completed %d." % k
def main(): #generate all the needed directories print("Create directories") if not os.path.exists(MODELS): os.makedirs(MODEL1) os.makedirs(MODEL2) if not os.path.exists(TEST_IMAGES): os.makedirs(TEST_IMAGES) if not os.path.exists(TRAINING_AUGMENTED): os.makedirs(AUGMENTED_IMAGES) os.makedirs(AUGMENTED_GNDTRUTH) if not os.path.exists(PREDICTIONS_TRAIN): os.makedirs(PTRAIN_IMAGES) os.makedirs(PTRAIN_GNDTRUTH) if not os.path.exists(PREDICTIONS_TEST): os.makedirs(PREDICTIONS_TEST) if not os.path.exists(POST_PROCESSING): os.makedirs(POST_PROCESSING) #generate augmented data print("Generate Augmented data") data_aug.data_augmentation(IMAGES_DIR, GT_DIR, AUGMENTED_IMAGES, AUGMENTED_GNDTRUTH) #copy augmented groundtruth in ptrain_gndtruth print("Copy groundtruth") copy_tree(AUGMENTED_GNDTRUTH, PTRAIN_GNDTRUTH) #flatten directory test_set_images for element in glob("{}test_*/*.png".format(TEST_DIR)): copy(element, TEST_IMAGES)
def data_aug(): num_examples = len(all_input_ids) num_token = 21128 id_a = -1 while True: id_a = (id_a + 1) % num_examples input_mask = all_input_mask[id_a] input_ids = all_input_ids[id_a] label_ids = all_label_ids[id_a] segment_ids = all_segment_ids[0] if FLAGS.data_aug: input_ids, input_mask, label_ids = data_augmentation( input_ids, input_mask, label_ids, seq_length, num_token) yield { 'input_ids': np.array(input_ids, dtype=np.int32), 'input_mask': np.array(input_mask, dtype=np.int32), 'label_ids': np.array(label_ids, dtype=np.int32), "segment_ids": np.array(segment_ids, dtype=np.int32) }
def train(config, anchors): num_epoch = int(config.epoch) log_dir = './results/' # Load dataset path = os.getcwd() train_file = path + '/train_tf_record' test_file = path + '/test_tf_record' train_dataset, val_dataset = make_dataset( BATCH_SIZE=config.training_batch_size, file_name=train_file, split=True) test_dataset = make_dataset(BATCH_SIZE=config.test_batch_size, file_name=test_file, split=False) # Model model = load_model(1, anchors, config.model_size, load_full_weights=config.load_full_weights) # set optimizer optimizer = tf.keras.optimizers.Adam(learning_rate=config.lr) # set Metrics tr_sum_loss = tf.keras.metrics.Mean() tr_iou = tf.keras.metrics.Mean() val_xy_loss = tf.keras.metrics.Mean() val_wh_loss = tf.keras.metrics.Mean() val_obj_loss = tf.keras.metrics.Mean() val_prob_loss = tf.keras.metrics.Mean() val_sum_loss = tf.keras.metrics.Mean() val_iou = tf.keras.metrics.Mean() test_iou = tf.keras.metrics.Mean() # Save Checkpoint ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=optimizer, net=model) manager = tf.train.CheckpointManager(ckpt, './tf_ckpts', max_to_keep=5) # Set up summary writers current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") tb_log_dir = log_dir + current_time summary_writer = tf.summary.create_file_writer(tb_log_dir) # Restore Checkpoint ckpt.restore(manager.latest_checkpoint) if manager.latest_checkpoint: logging.info('Restored from {}'.format(manager.latest_checkpoint)) else: logging.info('Initializing from scratch.') # calculate losses, update network and metrics. @tf.function def train_step(images, y_true): # Optimize the model with tf.GradientTape() as tape: detect0, detect1, detect2 = model(images, training=True, finetuning=config.finetuning) de_de0 = decode(detect0, anchors[2], 1, config.model_size) de_de1 = decode(detect1, anchors[1], 1, config.model_size) de_de2 = decode(detect2, anchors[0], 1, config.model_size) loss_de0 = yolo_loss(detect0, y_true, de_de0, anchors[2], config.model_size) loss_de1 = yolo_loss(detect1, y_true, de_de1, anchors[1], config.model_size) loss_de2 = yolo_loss(detect2, y_true, de_de2, anchors[0], config.model_size) total_loss = loss_de0 + loss_de1 + loss_de2 sum_loss = tf.reduce_sum(total_loss) grads = tape.gradient(sum_loss, model.trainable_variables) optimizer.apply_gradients(zip(grads, model.trainable_variables)) x = tf.concat([de_de0, de_de1, de_de2], axis=1) x = build_boxes(x) boxes_dicts = non_max_suppression(x, model.n_classes, config.max_out_size, config.iou_threshold, config.confid_threshold) return sum_loss, boxes_dicts @tf.function def val_step(images, y_true): detect0, detect1, detect2 = model(images, training=False) de_de0 = decode(detect0, anchors[2], 1, config.model_size) de_de1 = decode(detect1, anchors[1], 1, config.model_size) de_de2 = decode(detect2, anchors[0], 1, config.model_size) loss_de0 = yolo_loss(detect0, y_true, de_de0, anchors[2], config.model_size) loss_de1 = yolo_loss(detect1, y_true, de_de1, anchors[1], config.model_size) loss_de2 = yolo_loss(detect2, y_true, de_de2, anchors[0], config.model_size) total_loss = loss_de0 + loss_de1 + loss_de2 x = tf.concat([de_de0, de_de1, de_de2], axis=1) x = build_boxes(x) boxes_dicts = non_max_suppression(x, model.n_classes, config.max_out_size, config.iou_threshold, config.confid_threshold) return total_loss, boxes_dicts @tf.function def test_step(images): detect0, detect1, detect2 = model(images, training=False) de_de0 = decode(detect0, anchors[2], 1, config.model_size) de_de1 = decode(detect0, anchors[1], 1, config.model_size) de_de2 = decode(detect0, anchors[0], 1, config.model_size) x = tf.concat([de_de0, de_de1, de_de2], axis=1) x = build_boxes(x) boxes_dicts = non_max_suppression(x, model.n_classes, config.max_out_size, config.iou_threshold, config.confid_threshold) return boxes_dicts for epoch in range(num_epoch): begin = time.time() # Training loop for i in train_dataset: images = data_augmentation(i[0], config.probability, config.brightness_delta, config.contrast_range, config.hue_delta) y_true = i[1:] sum_loss, boxes_dicts = train_step(images, y_true) tr_sum_loss.update_state(sum_loss) pred_points = list(map(lambda x: x[0][..., 0:4], boxes_dicts)) pred_points = tf.concat(pred_points, axis=0) for _ in range(config.training_batch_size - tf.shape(pred_points)[0]): pred_points = tf.concat( values=[pred_points, tf.constant([[0., 0., 0., 0.]])], axis=0) training_iou_batch = tf.reduce_mean( iou(pred_points, tf.cast(tf.stack(y_true[0:4], axis=-1), dtype=tf.float32))) tr_iou.update_state(training_iou_batch) for j in val_dataset: images = j[0] y_true = j[1:] total_loss, boxes_dicts = val_step(images, y_true) val_xy_loss.update_state(total_loss[0]) val_wh_loss.update_state(total_loss[1]) val_obj_loss.update_state(total_loss[2]) val_prob_loss.update_state(total_loss[3]) val_sum_loss.update_state(tf.reduce_sum(total_loss)) pred_points = list(map(lambda x: x[0][..., 0:4], boxes_dicts)) pred_points = tf.concat(pred_points, axis=0) for _ in range(config.training_batch_size - tf.shape(pred_points)[0]): pred_points = tf.concat( values=[pred_points, tf.constant([[0., 0., 0., 0.]])], axis=0) val_iou_batch = tf.reduce_mean( iou(pred_points, tf.cast(tf.stack(y_true[0:4], axis=-1), dtype=tf.float32))) val_iou.update_state(val_iou_batch) with summary_writer.as_default(): tf.summary.scalar('Train Sum loss', tr_sum_loss.result(), step=epoch) tf.summary.scalar('Train IOU', tr_iou.result(), step=epoch) tf.summary.scalar('Validation XY loss', val_xy_loss.result(), step=epoch) tf.summary.scalar('Validation WH loss', val_wh_loss.result(), step=epoch) tf.summary.scalar('Validation OBJ loss', val_obj_loss.result(), step=epoch) tf.summary.scalar('Validation PROB loss', val_prob_loss.result(), step=epoch) tf.summary.scalar('Validation Sum loss', val_sum_loss.result(), step=epoch) tf.summary.scalar('Validation IOU', val_iou.result(), step=epoch) end = time.time() logging.info( "Epoch {:d} Training Sum loss: {:.3}, Training IOU: {:.3%} \n Validation Sum loss: {:.3}, " "Validation IOU: {:.3%}, Time:{:.5}s".format( epoch + 1, tr_sum_loss.result(), tr_iou.result(), val_sum_loss.result(), val_iou.result(), (end - begin))) tr_sum_loss.reset_states() tr_iou.reset_states() val_xy_loss.reset_states() val_wh_loss.reset_states() val_obj_loss.reset_states() val_prob_loss.reset_states() val_sum_loss.reset_states() val_iou.reset_states() if int(ckpt.step) % 5 == 0: save_path = manager.save() logging.info('Saved checkpoint for epoch {}: {}'.format( int(ckpt.step), save_path)) ckpt.step.assign_add(1) if epoch % 1 == 0: for j in test_dataset: images = j[0] y_true = j[1:] boxes_dicts = test_step(images) pred_points = list(map(lambda x: x[0][..., 0:4], boxes_dicts)) pred_points = tf.concat(pred_points, axis=0) for _ in range(config.test_batch_size - tf.shape(pred_points)[0]): pred_points = tf.concat( values=[pred_points, tf.constant([[0., 0., 0., 0.]])], axis=0) test_iou_batch = tf.reduce_mean( iou( pred_points, tf.cast(tf.stack(y_true[0:4], axis=-1), dtype=tf.float32))) test_iou.update_state(test_iou_batch) # test image visualization time_begin = time.time() test_sample = next(iter(test_dataset)) images = test_sample[0][0] y_true = list(map(lambda x: x[0], test_sample[1:])) # numpy array is necessary for openCV images_tf = tf.expand_dims(images, axis=0) images = tf.cast(images * 255, tf.uint8).numpy() images = cv.cvtColor(images, cv.COLOR_BGR2RGB) box_dicts = test_step(images_tf) draw_boxes_cv2(images, box_dicts, [y_true[4].numpy().decode('utf-8')], config.model_size, time_begin) cv.rectangle(images, (y_true[0], y_true[1]), (y_true[2], y_true[3]), (0, 0, 255), 2) images = cv.cvtColor(images, cv.COLOR_RGB2BGR) images = tf.expand_dims(images, axis=0) with summary_writer.as_default(): tf.summary.image('Test Object detection', images, step=epoch) tf.summary.scalar('Test IOU', test_iou.result(), step=epoch) logging.info("Test IOU: {:.3%}".format(test_iou.result())) test_iou.reset_states()
def backward(train_data,train_labels,train_num): with tf.Graph().as_default() as g: with tf.name_scope('input'): x = tf.placeholder(dtype=tf.float32,shape=[BATCH_SIZE,IMG_SIZE[0],IMG_SIZE[1],IMG_CHANNEL]) y_ = tf.placeholder(dtype=tf.float32,shape=[BATCH_SIZE,IMG_SIZE[0],IMG_SIZE[1],IMG_CHANNEL]) # forward y,output_Stokes,output_Stokes_GT,grad_output,grad_output_GT = model.forward(x,y_,True) # learning rate global_step = tf.Variable(0,trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step, train_num//BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) #loss function with tf.name_scope('loss'): mse = (1.0/BATCH_SIZE)*tf.nn.l2_loss(tf.subtract(y,y_)) mse_stokes = (1.0/BATCH_SIZE)*tf.nn.l2_loss(tf.subtract(output_Stokes, output_Stokes_GT)) mse_grad = (1.0/BATCH_SIZE)*tf.nn.l2_loss(tf.subtract(grad_output, grad_output_GT)) loss_init = mse + mse_stokes loss = mse + mse_stokes + 0.1*mse_grad #Optimizer # GradientDescent with tf.name_scope('train'): # Adam optimizer = tf.train.AdamOptimizer(learning_rate) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op_init = optimizer.minimize(loss_init, global_step=global_step) train_op = optimizer.minimize(loss,global_step=global_step) # Save model variables = tf.contrib.framework.get_variables_to_restore() variables_to_resotre = [v for v in variables if v.name.split('/')[0] != 'Gradient'] saver = tf.train.Saver(variables_to_resotre,max_to_keep=50) epoch = 0 config = tf.ConfigProto() with tf.Session(config=config) as sess: tf.global_variables_initializer().run() ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess,ckpt.model_checkpoint_path) epoch = int(ckpt.model_checkpoint_path.split('/')[-1].split('_')[-1].split('-')[-2]) while epoch<MAX_EPOCH: max_step = train_num//BATCH_SIZE listtmp = np.random.permutation(train_num) j = 0 for i in range(max_step): file =open("loss.txt",'a') ind = listtmp[j:j+BATCH_SIZE] j = j + BATCH_SIZE xs = train_data[ind,:,:,:] ys = train_labels[ind,:,:,:] mode = np.random.permutation(8) xs = DA.data_augmentation(xs,mode[0]) ys = DA.data_augmentation(ys,mode[0]) if epoch <50: _, loss_v, step = sess.run([train_op_init, loss_init, global_step], feed_dict={x: xs, y_: ys}) file.write("Epoch: %d Step is: %d After [ %d / %d ] training, the batch loss is %g.\n" % (epoch + 1, step, i + 1, max_step, loss_v)) file.close() else: _,loss_v,step = sess.run([train_op,loss,global_step],feed_dict={x:xs, y_:ys}) file.write("Epoch: %d Step is: %d After [ %d / %d ] training, the batch loss is %g.\n" % (epoch + 1, step, i + 1, max_step, loss_v)) file.close() #print("Epoch: %d After [ %d / %d ] training, the batch loss is %g." % (epoch + 1, i + 1, max_step, loss_v)) saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME+'_epoch_'+str(epoch+1)),global_step = global_step) epoch +=1
def train(hparams, num_epoch, tuning): log_dir = './results/' test_batch_size = 8 # Load dataset training_set, valid_set = make_dataset(BATCH_SIZE=hparams['HP_BS'], file_name='train_tf_record', split=True) test_set = make_dataset(BATCH_SIZE=test_batch_size, file_name='test_tf_record', split=False) class_names = ['NRDR', 'RDR'] # Model model = ResNet() # set optimizer optimizer = tf.keras.optimizers.Adam(learning_rate=hparams['HP_LR']) # set metrics train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy() valid_accuracy = tf.keras.metrics.Accuracy() valid_con_mat = ConfusionMatrix(num_class=2) test_accuracy = tf.keras.metrics.Accuracy() test_con_mat = ConfusionMatrix(num_class=2) # Save Checkpoint if not tuning: ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=optimizer, net=model) manager = tf.train.CheckpointManager(ckpt, './tf_ckpts', max_to_keep=5) # Set up summary writers current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") tb_log_dir = log_dir + current_time + '/train' summary_writer = tf.summary.create_file_writer(tb_log_dir) # Restore Checkpoint if not tuning: ckpt.restore(manager.latest_checkpoint) if manager.latest_checkpoint: logging.info('Restored from {}'.format(manager.latest_checkpoint)) else: logging.info('Initializing from scratch.') @tf.function def train_step(train_img, train_label): # Optimize the model loss_value, grads = grad(model, train_img, train_label) optimizer.apply_gradients(zip(grads, model.trainable_variables)) train_pred, _ = model(train_img) train_label = tf.expand_dims(train_label, axis=1) train_accuracy.update_state(train_label, train_pred) for epoch in range(num_epoch): begin = time() # Training loop for train_img, train_label, train_name in training_set: train_img = data_augmentation(train_img) train_step(train_img, train_label) with summary_writer.as_default(): tf.summary.scalar('Train Accuracy', train_accuracy.result(), step=epoch) for valid_img, valid_label, _ in valid_set: valid_img = tf.cast(valid_img, tf.float32) valid_img = valid_img / 255.0 valid_pred, _ = model(valid_img, training=False) valid_pred = tf.cast(tf.argmax(valid_pred, axis=1), dtype=tf.int64) valid_con_mat.update_state(valid_label, valid_pred) valid_accuracy.update_state(valid_label, valid_pred) # Log the confusion matrix as an image summary cm_valid = valid_con_mat.result() figure = plot_confusion_matrix(cm_valid, class_names=class_names) cm_valid_image = plot_to_image(figure) with summary_writer.as_default(): tf.summary.scalar('Valid Accuracy', valid_accuracy.result(), step=epoch) tf.summary.image('Valid ConfusionMatrix', cm_valid_image, step=epoch) end = time() logging.info( "Epoch {:d} Training Accuracy: {:.3%} Validation Accuracy: {:.3%} Time:{:.5}s" .format(epoch + 1, train_accuracy.result(), valid_accuracy.result(), (end - begin))) train_accuracy.reset_states() valid_accuracy.reset_states() valid_con_mat.reset_states() if not tuning: if int(ckpt.step) % 5 == 0: save_path = manager.save() logging.info('Saved checkpoint for epoch {}: {}'.format( int(ckpt.step), save_path)) ckpt.step.assign_add(1) for test_img, test_label, _ in test_set: test_img = tf.cast(test_img, tf.float32) test_img = test_img / 255.0 test_pred, _ = model(test_img, training=False) test_pred = tf.cast(tf.argmax(test_pred, axis=1), dtype=tf.int64) test_accuracy.update_state(test_label, test_pred) test_con_mat.update_state(test_label, test_pred) cm_test = test_con_mat.result() # Log the confusion matrix as an image summary figure = plot_confusion_matrix(cm_test, class_names=class_names) cm_test_image = plot_to_image(figure) with summary_writer.as_default(): tf.summary.scalar('Test Accuracy', test_accuracy.result(), step=epoch) tf.summary.image('Test ConfusionMatrix', cm_test_image, step=epoch) logging.info("Trained finished. Final Accuracy in test set: {:.3%}".format( test_accuracy.result())) # Visualization if not tuning: for vis_img, vis_label, vis_name in test_set: vis_label = vis_label[0] vis_name = vis_name[0] vis_img = tf.cast(vis_img[0], tf.float32) vis_img = tf.expand_dims(vis_img, axis=0) vis_img = vis_img / 255.0 with tf.GradientTape() as tape: vis_pred, conv_output = model(vis_img, training=False) pred_label = tf.argmax(vis_pred, axis=-1) vis_pred = tf.reduce_max(vis_pred, axis=-1) grad_1 = tape.gradient(vis_pred, conv_output) weight = tf.reduce_mean(grad_1, axis=[1, 2]) / grad_1.shape[1] act_map0 = tf.nn.relu( tf.reduce_sum(weight * conv_output, axis=-1)) act_map0 = tf.squeeze(tf.image.resize(tf.expand_dims(act_map0, axis=-1), (256, 256), antialias=True), axis=-1) plot_map(vis_img, act_map0, vis_pred, pred_label, vis_label, vis_name) break return test_accuracy.result()
def backward(inputs, labels, train_num): with tf.Graph().as_default() as g: with tf.name_scope('input'): x = tf.placeholder(tf.float32, [None, IMG_SIZE[0], IMG_SIZE[1], IMG_CHANNEL]) y_ = tf.placeholder(tf.int64, [None, IMG_SIZE[0], IMG_SIZE[1], 1]) with_dropout = tf.placeholder(tf.bool, name="with_dropout") keep_prob = tf.placeholder(tf.float32, shape=None, name="keep_rate") batch_size = tf.placeholder(tf.int64, shape=[], name="batch_size") global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, train_num // BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) logits = model.forward(x, True, with_dropout, keep_prob, batch_size) with tf.name_scope('loss'): loss, accuracy = normal_loss(logits, y_, model.Num_Classes) optimizer = tf.train.AdamOptimizer(learning_rate) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step=global_step) # Save model saver = tf.train.Saver(max_to_keep=50) epoch = 0 with tf.Session() as sess: tf.global_variables_initializer().run() ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) epoch = int( ckpt.model_checkpoint_path.split('/')[-1].split('_') [-1].split('-')[-2]) while epoch < MAX_EPOCH: max_step = train_num // BATCH_SIZE listtmp = np.random.permutation(train_num) j = 0 for i in range(max_step): file = open("loss.txt", 'a') ind = listtmp[j:j + BATCH_SIZE] j = j + BATCH_SIZE xs = inputs[ind, :, :, :] ys = labels[ind, :, :, :] mode = np.random.permutation(8) xs = DA.data_augmentation(xs, mode[0]) ys = DA.data_augmentation(ys, mode[0]) _, loss_v, accu, step = sess.run( [train_op, loss, accuracy, global_step], feed_dict={ x: xs, y_: ys, with_dropout: True, keep_prob: 0.5, batch_size: BATCH_SIZE }) file.write( "Epoch: %d, After [%d / %d] training, the batch loss is %g, the accuracy is %g.\n" % (epoch, i + 1, max_step, loss_v, accu)) file.close() saver.save(sess, os.path.join( MODEL_SAVE_PATH, MODEL_NAME + '_epoch_' + str(epoch + 1)), global_step=global_step) epoch += 1
def make_folds(fold1_directory, fold2_directory, fold3_directory, fold4_directory, n_chunks, mode): queen_1 = [] features_1 = [] for filename in os.listdir(fold1_directory): if filename.endswith(".wav"): filepath = os.path.join(fold1_directory, filename) out = ft.feature_extraction(filepath, n_chunks, mode) features_1.append(out) q = ft.queen_info(filepath) queen_1.append(q) features_1 = np.asarray(features_1) queen_1 = np.asarray(queen_1) queen_2 = [] features_2 = [] for filename in os.listdir(fold2_directory): if filename.endswith(".wav"): filepath = os.path.join(fold2_directory, filename) out = ft.feature_extraction(filepath, n_chunks, mode) features_2.append(out) q = ft.queen_info(filepath) queen_2.append(q) features_2 = np.asarray(features_2) queen_2 = np.asarray(queen_2) queen_3 = [] features_3 = [] for filename in os.listdir(fold3_directory): if filename.endswith(".wav"): filepath = os.path.join(fold3_directory, filename) out = ft.feature_extraction(filepath, n_chunks, mode) features_3.append(out) q = ft.queen_info(filepath) queen_3.append(q) features_3 = np.asarray(features_3) queen_3 = np.asarray(queen_3) queen_4 = [] features_4 = [] for filename in os.listdir(fold4_directory): if filename.endswith(".wav"): filepath = os.path.join(fold4_directory, filename) out = ft.feature_extraction(filepath, n_chunks, mode) features_4.append(out) q = ft.queen_info(filepath) queen_4.append(q) features_4 = np.asarray(features_4) queen_4 = np.asarray(queen_4) #data augmentation - for conducting experiment without data augmentation, # comment lines below and uncomment block "no data augmentation" (line 98) queen_aug = [] features_aug = [] for filename in os.listdir(fold1_directory): filepath = os.path.join(fold1_directory, filename) out = da.data_augmentation(filepath, n_chunks) features_aug.append(out) q = ft.queen_info(filepath) queen_aug.append(q) for filename in os.listdir(fold2_directory): filepath = os.path.join(fold2_directory, filename) out = da.data_augmentation(filepath, n_chunks) features_aug.append(out) q = ft.queen_info(filepath) queen_aug.append(q) for filename in os.listdir(fold3_directory): filepath = os.path.join(fold3_directory, filename) out = da.data_augmentation(filepath, n_chunks) features_aug.append(out) q = ft.queen_info(filepath) queen_aug.append(q) features_aug = np.asarray(features_aug) queen_aug = np.asarray(queen_aug) X1_train = np.concatenate( (features_2, features_3, features_4, features_aug)) X2_train = np.concatenate( (features_1, features_3, features_4, features_aug)) X3_train = np.concatenate( (features_1, features_2, features_4, features_aug)) X4_train = np.concatenate( (features_2, features_3, features_1, features_aug)) Y1_train = np.concatenate((queen_2, queen_3, queen_4, queen_aug)) Y2_train = np.concatenate((queen_1, queen_3, queen_4, queen_aug)) Y3_train = np.concatenate((queen_1, queen_2, queen_4, queen_aug)) Y4_train = np.concatenate((queen_2, queen_3, queen_1, queen_aug)) #--------------------------------------------------------------# #no data augmentation # X1_train = np.concatenate((features_2, features_3, features_4)) # X2_train = np.concatenate((features_1, features_3, features_4)) # X3_train = np.concatenate((features_1, features_2, features_4)) # X4_train = np.concatenate((features_2, features_3, features_1)) # # Y1_train = np.concatenate((queen_2, queen_3, queen_4)) # Y2_train = np.concatenate((queen_1, queen_3, queen_4)) # Y3_train = np.concatenate((queen_1, queen_2, queen_4)) # Y4_train = np.concatenate((queen_2, queen_3, queen_1)) #--------------------------------------------------------------# X1_test = features_1 X2_test = features_2 X3_test = features_3 X4_test = features_4 Y1_test = queen_1 Y2_test = queen_2 Y3_test = queen_3 Y4_test = queen_4 return X1_train, X2_train, X3_train, X4_train, X1_test, X2_test, X3_test, X4_test, Y1_train, Y2_train, Y3_train, Y4_train, Y1_test, Y2_test, Y3_test, Y4_test
# Down sampling if down_sampling: X = X[:,::d_s_factor,:] if lowpass_flag: lp = lowpass(X) X = lp.butter_lowpass_filter(cutoff=3,fs=25,order=5) # split X_train, X_test, y_train, y_test, idx1, idx2 = train_test_split(X, y, indices, test_size=0.4) if feature_extraction_flag: c, K = 1, 'linear' # Data Augmentation if data_augmentation_flag: da = data_augmentation(X_train, y_train) X_train, y_train = da.adding_noise(mu, sigma, factor) X_train, y_train = da.shift_side(steps = [50,100]) X_train, y_train = da.window_warping(200, 300, window_ratio = [[1,2],[2,1]]) fe = feature_extraction(X_train) X_train, index, cross_corelation = fe.feature_extraction() fe = feature_extraction(X_test) X_test, index, cross_corelation = fe.feature_extraction() fe = feature_extraction(X) X,_,__ = fe.feature_extraction() # train n_timesteps, n_features, axis = X_train.shape X_traind_2d = X_train.reshape((n_timesteps,n_features*axis))
def mk_data_augmented(x_y_label, patient, datagen): train_not_cancer_arr = [] train_not_cancer_label = [] train_cancer_arr = [] train_cancer_label = [] test_not_cancer_arr = [] test_not_cancer_label = [] test_cancer_arr = [] test_cancer_label = [] datagen = datagen for i in range(len(x_y_label)): if x_y_label[i][1] == 0 and x_y_label[i][2] != patient: train_not_cancer_arr.append(x_y_label[i][0]) train_not_cancer_label.append(x_y_label[i][1]) elif x_y_label[i][1] == 1 and x_y_label[i][2] != patient: train_cancer_arr.append(x_y_label[i][0]) train_cancer_label.append(x_y_label[i][1]) elif x_y_label[i][1] == 0 and x_y_label[i][2] == patient: test_not_cancer_arr.append(x_y_label[i][0]) test_not_cancer_label.append(x_y_label[i][1]) elif x_y_label[i][1] == 1 and x_y_label[i][2] == patient: test_cancer_arr.append(x_y_label[i][0]) test_cancer_label.append(x_y_label[i][1]) train_not_cancer = data_augmentation(train_not_cancer_arr, train_not_cancer_label, datagen) train_cancer = data_augmentation(train_cancer_arr, train_cancer_label, datagen) test_not_cancer = data_augmentation(test_not_cancer_arr, test_not_cancer_label, datagen) test_cancer = data_augmentation(test_cancer_arr, test_cancer_label, datagen) #ラベルの比率合わせ train_length_min = min([len(train_cancer), len(train_not_cancer)]) test_length_min = min([len(test_cancer), len(test_not_cancer)]) train_cancer = random.sample(train_cancer, train_length_min) train_not_cancer = random.sample(train_not_cancer, train_length_min) test_cancer = random.sample(test_cancer, test_length_min) test_not_cancer = random.sample(test_not_cancer, test_length_min) train_cancer.extend(train_not_cancer) test_cancer.extend(test_not_cancer) X_train, Y_train = list(zip(*train_cancer)) X_test, Y_test = list(zip(*test_cancer)) X_train = np.asarray(X_train) X_test = np.asarray(X_test) Y_train = np.asarray(Y_train) Y_test = np.asarray(Y_test) from keras.utils.np_utils import to_categorical x_train = np.array(X_train).astype('float32') y_train = to_categorical(Y_train, 2) x_test = np.array(X_test).astype('float32') y_test = to_categorical(Y_test, 2) img_rows, img_cols = 100, 100 # shapeの調整 if K.image_data_format() == 'channels_first': X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols) X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3) input_shape = (img_rows, img_cols, 1) #validation_dataの作成(train_dataの0.5割をvalidationとしている) from sklearn.model_selection import train_test_split val_x_train, val_x_test, val_y_train, val_y_test = train_test_split( x_train, y_train, test_size=0.5, random_state=12345) return Y_test, X_train, x_test, y_test, val_x_train, val_y_train, val_x_test, val_y_test