def init_tfdata(self, batch_size, main_dir, resize_shape, mode='train'): self.data_session = tf.Session() print("Creating the iterator for training data") with tf.device('/cpu:0'): segdl = SegDataLoader(main_dir, batch_size, (resize_shape[0], resize_shape[1]), resize_shape, # * 2), resize_shape, 'data/cityscapes_tfdata/train.txt') iterator = Iterator.from_structure(segdl.data_tr.output_types, segdl.data_tr.output_shapes) next_batch = iterator.get_next() self.init_op = iterator.make_initializer(segdl.data_tr) self.data_session.run(self.init_op) print("Loading Validation data in memoryfor faster training..") self.val_data = {'X': np.load(self.args.data_dir + "X_val.npy"), 'Y': np.load(self.args.data_dir + "Y_val.npy")} # self.crop() # import cv2 # cv2.imshow('crop1', self.val_data['X'][0,:,:,:]) # cv2.imshow('crop2', self.val_data['X'][1,:,:,:]) # cv2.imshow('seg1', self.val_data['Y'][0,:,:]) # cv2.imshow('seg2', self.val_data['Y'][1,:,:]) # cv2.waitKey() self.val_data_len = self.val_data['X'].shape[0] - self.val_data['X'].shape[0] % self.args.batch_size # self.num_iterations_validation_per_epoch = ( # self.val_data_len + self.args.batch_size - 1) // self.args.batch_size self.num_iterations_validation_per_epoch = self.val_data_len // self.args.batch_size print("Val-shape-x -- " + str(self.val_data['X'].shape) + " " + str(self.val_data_len)) print("Val-shape-y -- " + str(self.val_data['Y'].shape)) print("Num of iterations on validation data in one epoch -- " + str(self.num_iterations_validation_per_epoch)) print("Validation data is loaded") return next_batch, segdl.data_len
def get_dataset_ops(data_train, data_val, batch_size, train_size, val_size, shuffle=True): """ """ # shuffle the dataset and create batches if shuffle: data_train = data_train.shuffle(train_size) data_val = data_val.shuffle(val_size) data_train = data_train.batch(batch_size) data_val = data_val.batch(batch_size) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(data_train.output_types, data_train.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators init_op_train = iterator.make_initializer(data_train) init_op_val = iterator.make_initializer(data_val) return init_op_train, init_op_val, next_batch
def get_dataset(self, n_subset=-1): cfg = self.cfg train_dataset, valid_dataset = self.get_torch_dataset() if n_subset > 0: train_dataset = Subset(train_dataset, list(range(100))) valid_dataset = Subset(valid_dataset, list(range(100))) n_train_iteration = len(train_dataset) // cfg.batch_size n_valid_iteration = len(valid_dataset) // cfg.batch_size train_dataset = self.to_tf_dataset(train_dataset, shuffle=True) valid_dataset = self.to_tf_dataset(valid_dataset, shuffle=False) iterator = Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes) train_init_op = iterator.make_initializer(train_dataset) valid_init_op = iterator.make_initializer(valid_dataset) input_tensor = iterator.get_next() return ( input_tensor, (train_init_op, valid_init_op), (n_train_iteration, n_valid_iteration), )
def make_tf_dataset(file_path='', batch_size=10): loaded_data = np.load(file_path) X_train = loaded_data['X_train'] X_test = loaded_data['X_test'] Y_train = loaded_data['Y_train'] Y_test = loaded_data['Y_test'] print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape, flush=True) X_train = tf.cast(X_train, tf.float32) X_test = tf.cast(X_test, tf.float32) Y_train = tf.cast(Y_train, tf.int32) Y_test = tf.cast(Y_test, tf.int32) train_dat = Dataset.from_tensor_slices((X_train, Y_train)) train_dat = train_dat.batch(batch_size) test_dat = Dataset.from_tensor_slices((X_test, Y_test)) test_dat = test_dat.batch(batch_size) data_dict = {} iterator = Iterator.from_structure(train_dat.output_types, train_dat.output_shapes) data_dict['iterator'] = iterator data_dict['train_it_init'] = iterator.make_initializer(train_dat) data_dict['test_it_init'] = iterator.make_initializer(test_dat) #data_dict['train_it'] = train_iterator #data_dict['test_it'] = test_iterator #data_dict['train_it_init'] = train_iterator.initializer #data_dict['test_it_init'] = test_iterator.initializer return data_dict
def train_mnist_single_machine(num_epochs, use_fake_data=False, device=None, manual_op_exec=False): """Train a ConvNet on MNIST. Args: num_epochs: int. Number of passes to make over the training set. use_fake_data: bool. If True, generate a synthetic dataset. device: string or None. The covariance and inverse update ops are run on this device. If empty or None, the default device will be used. (Default: None) manual_op_exec: bool, If `True` then `minimize_loss_single_machine_manual` is called for training which handles inverse and covariance computation. This is shown only for illustrative purpose. Otherwise `minimize_loss_single_machine` is called which relies on `PeriodicInvCovUpdateOpt` for op placement and execution. Returns: accuracy of model on the final minibatch of training data. """ from tensorflow.data import Iterator # Load a dataset. print ("Loading MNIST into memory.") tf.logging.info("Loading MNIST into memory.") iter_train_handle, output_types, output_shapes = mnist.load_mnist_as_iterator(num_epochs, args.batch_size, train=True, use_fake_data=use_fake_data, flatten_images=False) iter_val_handle, _, _ = mnist.load_mnist_as_iterator(10000*num_epochs, # This just ensures this doesn't cause early termination 10000, train=False, use_fake_data=use_fake_data, flatten_images=False) handle = tf.placeholder(tf.string, shape=[]) iterator = Iterator.from_string_handle( handle, output_types, output_shapes) next_batch = iterator.get_next() (examples, labels) = next_batch # Build a ConvNet. layer_collection = kfac.LayerCollection() loss, accuracy = build_model( examples, labels, num_labels=10, layer_collection=layer_collection, register_layers_manually=_USE_MANUAL_REG) if not _USE_MANUAL_REG: layer_collection.auto_register_layers() # Without setting allow_soft_placement=True there will be problems when # the optimizer tries to place certain ops like "mod" on the GPU (which isn't # supported). config = tf.ConfigProto(allow_soft_placement=True) # Fit model. return minimize_loss_single_machine(handle, iter_train_handle, iter_val_handle, loss, accuracy, layer_collection, device=device, session_config=config)
def loaddata(self): """ loads data queue pipelines for train,test,val which can be switched during run time. """ #change image to float scale = (1.0 / 255.0) #mean centering mean = np.load("DataCollection/Pong-v0/mean.npy") # making multi threaded pipelines from tfrecords using feedable iterators to switch queues during run time filenames = glob.glob('DataCollection/Pong-v0/'+'train'+'/*.tfrecords') datasettrain = tf.data.TFRecordDataset(filenames) filenames = glob.glob('DataCollection/Pong-v0/' + 'val' + '/*.tfrecords') datasetval = tf.data.TFRecordDataset(filenames) filenames = glob.glob('DataCollection/Pong-v0/' + 'test' + '/*.tfrecords') datasettest = tf.data.TFRecordDataset(filenames) #restore image and action data and shape datasettrain = datasettrain.map(map_func= self.parse_function, num_parallel_calls= 4) #to iterate over dataset for multiple epochs datasettrain = datasettrain.repeat() #shuffling and creating a queue buffer datasettrain = datasettrain.shuffle(buffer_size=1000) # specify queue size buffer #making batches datasettrain = datasettrain.batch(32) #string handle for train iter_train_handle = datasettrain.make_one_shot_iterator().string_handle() datasetval = datasetval.map(map_func=self.parse_function, num_parallel_calls=4) datasetval = datasetval.repeat() datasetval = datasetval.shuffle(buffer_size=1000) # specify queue size buffer datasetval = datasetval.batch(32) # string handle for val iter_val_handle = datasetval.make_one_shot_iterator().string_handle() datasettest = datasettest.map(map_func=self.parse_function, num_parallel_calls=4) datasettest = datasettest.repeat() datasettest = datasettest.shuffle(buffer_size=1000) # specify queue size buffer datasettest = datasettest.batch(32) # string handle for test iter_test_handle = datasettest.make_one_shot_iterator().string_handle() #handle to indicate which queue we want to switch to or iterate over handle = tf.placeholder(tf.string, shape=[]) iterator = Iterator.from_string_handle(handle, datasettrain.output_types, datasettrain.output_shapes) #get data tensors s_t_batch, a_t_batch, x_t_1_batch = iterator.get_next() mean_const = tf.constant(mean, dtype=tf.float32) #mean centering and convert to float s_t_batch = (s_t_batch - tf.tile(mean_const, [1, 1, 4])) * scale x_t_1_batch = (x_t_1_batch - mean_const) * scale self.s_t_batch = s_t_batch self.a_t_batch = a_t_batch self.x_t_1_batch = x_t_1_batch self.iter_train_handle = iter_train_handle self.iter_val_handle = iter_val_handle self.iter_test_handle = iter_test_handle self.handle = handle
def create_optimization_step_and_data_generator(self): learn_rate = self.config_train.get('learning_rate', 1e-3) vars_fixed = self.config_train.get('vars_not_update', None) vars_update = self.get_variable_list(vars_fixed, include=False) update_ops = tf.get_collection( tf.GraphKeys.UPDATE_OPS) # for batch normalization with tf.control_dependencies(update_ops): self.opt_step = tf.train.AdamOptimizer(learn_rate).minimize( self.loss, var_list=vars_update) # Place data loading and preprocessing on the cpu with tf.device('/cpu:0'): self.train_data = ImageDataGenerator( self.config_data['data_train'], self.config_sampler) # create an reinitializable iterator given the dataset structure train_iterator = Iterator.from_structure( self.train_data.data.output_types, self.train_data.data.output_shapes) self.next_train_batch = train_iterator.get_next() # Ops for initializing the two different iterators self.train_init_op = train_iterator.make_initializer( self.train_data.data) valid_data = [] next_valid_batch = [] valid_init_op = [] for i in range(len(self.config_data) - 1): with tf.device('/cpu:0'): temp_valid_data = ImageDataGenerator( self.config_data["data_valid{0:}".format(i)], self.config_sampler) temp_valid_iterator = Iterator.from_structure( temp_valid_data.data.output_types, temp_valid_data.data.output_shapes) temp_next_valid_batch = temp_valid_iterator.get_next() temp_valid_init_op = temp_valid_iterator.make_initializer( temp_valid_data.data) valid_data.append(temp_valid_data) next_valid_batch.append(temp_next_valid_batch) valid_init_op.append(temp_valid_init_op) self.valid_data = valid_data self.next_valid_batch = next_valid_batch self.valid_init_op = valid_init_op
def inference(model_path, list_file, image_size, output_dim, batch_size): with tf.device('/cpu:0'): val_data = DataGenerator(list_file, image_size, output_dim, mode='inference', batch_size=batch_size, shuffle=False) # Create an reinitializable iterator given the data structure iterator = Iterator.from_structure(val_data.data.output_types, val_data.data.output_shapes) next_batch = iterator.get_next() val_init_op = iterator.make_initializer(val_data.data) val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size)) saver = tf.train.import_meta_graph(model_path + '.meta') graph = tf.get_default_graph() # with tf.get_default_graph() as graph: x = graph.get_tensor_by_name("input_data:0") out = graph.get_tensor_by_name("dense5/output_logits:0") # training = graph.get_tensor_by_name("training:0") with tf.Session() as sess: saver.restore(sess, model_path) sess.run(val_init_op) loss_list = [] for i in range(val_batches_per_epoch): img_batch, label_batch = sess.run(next_batch) np.savetxt('label.txt', label_batch) preds = sess.run(out, feed_dict={x: img_batch}) # training: False}) np.savetxt('pred.txt', preds) loss = l2_loss(preds, label_batch) loss_list.append(loss) average_loss = sum(loss_list) / len(loss_list) # print(loss_list) return average_loss
def predict(model_path, list_file, image_size, output_dim, batch_size): with tf.device('/cpu:0'): val_data = DataGenerator(list_file, image_size, output_dim, mode='inference', batch_size=batch_size, shuffle=False) # Create an reinitializable iterator given the data structure iterator = Iterator.from_structure(val_data.data.output_types, val_data.data.output_shapes) next_batch = iterator.get_next() val_init_op = iterator.make_initializer(val_data.data) val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size)) model = ResNet([image_size, image_size, 3], output_dim, basic_block, [3, 4, 6, 3]) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, model_path) sess.run(val_init_op) loss_list = [] for i in range(val_batches_per_epoch): img_batch, label_batch = sess.run(next_batch) preds = sess.run(model.predicts, feed_dict={ model.images: img_batch, model.training: False }) loss = asym_l2_loss(preds, label_batch) loss_list.append(loss) average_loss = sum(loss_list) / len(loss_list) return average_loss
def load_data_sets(output_dim, batch_size, train_list, val_list, base_dir): # Place data loading and pre-processing on cpu with tf.device('/cpu:0'): train_data = DataGenerator(base_dir, train_list, output_dim, 'training', batch_size, shuffle=True) val_data = DataGenerator(base_dir, val_list, output_dim, 'inference', batch_size, shuffle=False) # Create an reinitializable iterator given the data structure iterator = Iterator.from_structure(train_data.data.output_types, train_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators train_init_op = iterator.make_initializer(train_data.data) val_init_op = iterator.make_initializer(val_data.data) train_member = DataMember(init_op=train_init_op, num_examples=train_data.data_size, batch_size=batch_size) val_member = DataMember(init_op=val_init_op, num_examples=val_data.data_size, batch_size=batch_size) return DataFamily(train=train_member, val=val_member, next_batch=next_batch)
def model_train(train_imgs, train_labels, val_imgs, val_labels): # Learning params init_lr = 0.01 lr_decay = 0.2 epoch_decay = 50 num_epochs = 100 batch_size = 50 # Network params dropout_rate = 0.5 num_classes = 2 # How often we want to write the tf.summary data to disk display_step = 15 # Path for tf.summary.FileWriter and to store model checkpoints current_dir = os.getcwd() filewriter_path = os.path.join(current_dir, 'log', 'cls_tensorboard') checkpoint_path = os.path.join(current_dir, 'log', 'cls_checkpoints') #-- Main Part of the finetuning Script. # Create parent path if it doesn't exist if not os.path.isdir(checkpoint_path): os.makedirs(checkpoint_path, mode=0755) # Place data loading and preprocessing on the cpu with tf.device('/cpu:0'): tr_data = ImageDataGenerator(train_imgs, train_labels, mode='training', batch_size=batch_size, num_classes=num_classes, shuffle=True) val_data = ImageDataGenerator(val_imgs, val_labels, mode='inference', batch_size=batch_size, num_classes=num_classes, shuffle=False) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) #test_init_op = iterator.make_initializer(test_data.data) # TF placeholder for graph input and output x = tf.placeholder(tf.float32, [batch_size, 224, 224, 3]) y = tf.placeholder(tf.float32, [batch_size, num_classes]) is_train = tf.placeholder(tf.bool, name='is_train') #keep_prob = tf.placeholder(tf.float32) lr = tf.placeholder(tf.float32) # Initialize model model = ResNet(x, num_classes, [], is_train) # Link variable to model output score = model.logits # List of trainable variables of the layers we want to train var_list = [v for v in tf.trainable_variables()] # Op for calculating the loss with tf.name_scope("cross_ent"): logits = tf.nn.softmax_cross_entropy_with_logits(logits=score, labels=y) loss = tf.reduce_mean(logits) # Train op update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.name_scope("train"): # Get gradients of all trainable variables gradients = tf.gradients(loss, var_list) gradients = list(zip(gradients, var_list)) # Create optimizer and apply gradient descent to the trainable variables #optimizer = tf.train.GradientDescentOptimizer(learning_rate) #with tf.control_dependencies(update_ops): # train_op = optimizer.apply_gradients(grads_and_vars=gradients) #optimizer = tf.train.AdamOptimizer(learning_rate) optimizer = tf.train.MomentumOptimizer(lr, 0.9) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, var_list=var_list) # Add gradients to summary for gradient, var in gradients: tf.summary.histogram(var.name + '/gradient', gradient) # Add the variables we train to the summary for var in var_list: tf.summary.histogram(var.name, var) # Add the loss to summary tf.summary.scalar('cross-ent', loss) # Evaluation op: Accuracy of the model with tf.name_scope('accuracy'): pred_label = tf.argmax(score, 1) true_label = tf.argmax(y, 1) correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) tf.summary.scalar('accuracy', accuracy) # Merge all summaries together merged_summary = tf.summary.merge_all() # Initialize the FileWriter writer = tf.summary.FileWriter(filewriter_path) # Initialize an saver for store model checkpoints saver = tf.train.Saver() # Get the number of training/validation steps per epoch train_batches_per_epoch = int(np.floor(tr_data.data_size / batch_size)) val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size)) #test_batches_per_epoch = int(np.floor(test_data.data_size / batch_size)) print 'Train data batches per epoch: %s' % (train_batches_per_epoch) print 'Val data batches per epoch: %s' % (val_batches_per_epoch) # Start Tensorflow session with tf.Session() as sess: # Initialize all variables sess.run(tf.global_variables_initializer()) # Add the model graph to TensorBoard writer.add_graph(sess.graph) print("{} Start training...".format(datetime.now())) print("{} Open Tensorboard at --logdir {}".format( datetime.now(), filewriter_path)) test_acc_list = [] # Loop over number of epochs for epoch in range(num_epochs): print("{} Epoch number: {}".format(datetime.now(), epoch + 1)) current_lr = lr_decay**((epoch + 1) / epoch_decay) * init_lr # Initialize iterator with the training dataset sess.run(training_init_op) for step in range(train_batches_per_epoch): # get next batch of data img_batch, label_batch = sess.run(next_batch) # And run the training op sess.run(train_op, feed_dict={ x: img_batch, y: label_batch, is_train: True, lr: current_lr }) # Generate summary with the current batch of data and write to file if step % display_step == 0: s = sess.run(merged_summary, feed_dict={ x: img_batch, y: label_batch, is_train: False, lr: current_lr }) writer.add_summary(s, epoch * train_batches_per_epoch + step) # Test the model on the entire training set to check over-fitting print("{} Start validation".format(datetime.now())) val_acc = 0. val_count = 0 sess.run(training_init_op) for _ in range(train_batches_per_epoch): img_batch, label_batch = sess.run(next_batch) acc = sess.run(accuracy, feed_dict={ x: img_batch, y: label_batch, is_train: False, lr: current_lr }) val_acc += acc val_count += 1 val_acc /= val_count print("{} Validation Accuracy = {:.4f}".format( datetime.now(), val_acc)) # Test the model on the entire test set print("{} Start test".format(datetime.now())) sess.run(validation_init_op) test_acc = 0. test_count = 0 preds = [] trues = [] for _ in range(val_batches_per_epoch): img_batch, label_batch = sess.run(next_batch) acc, pl, tl = sess.run([accuracy, pred_label, true_label], feed_dict={ x: img_batch, y: label_batch, is_train: False, lr: current_lr }) test_acc += acc test_count += 1 preds = np.concatenate((preds, pl)) trues = np.concatenate((trues, tl)) test_acc /= test_count print("{} Test Accuracy = {:.4f}".format(datetime.now(), test_acc)) print 'Confusion matrix' cm = sess.run(tf.confusion_matrix(preds, trues)) print cm test_acc_list.append(test_acc) with open('resnet_test_acc.csv', 'a') as f: f.write(','.join([str(item) for item in test_acc_list]) + '\n')
def train(args): num_classes = 1000 tr_data = ImageDataGenerator('../data/ImageNet/trainDataPath.txt', dataroot='/ImageNet/train/', mode='training', batch_size=args.batch_size, num_classes=num_classes, shuffle=False) val_data = ImageDataGenerator('../data/ImageNet/valDataPath.txt', dataroot='/ImageNet/val/', mode='inference', batch_size=args.batch_size, num_classes=num_classes, shuffle=False) iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) train_batches_per_epoch = int(np.floor(tr_data.data_size / args.batch_size)) val_batches_per_epoch = int(np.floor(val_data.data_size / args.batch_size)) x = tf.placeholder(tf.float32, (None, 227, 227, 3)) y = tf.placeholder(tf.float32, (None, num_classes)) model = AlexNet(x, y, args) optimizer1 = tf.train.AdamOptimizer(1e-5) first_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "cnn") first_train_op = optimizer1.minimize(model.loss, var_list=first_train_vars) if args.adv_flag: optimizer2 = tf.train.AdamOptimizer(1e-4) second_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "adv") second_train_op = optimizer2.minimize(model.adv_loss, var_list=second_train_vars) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('Starting training') print('load AlexNet weights') model.load_initial_weights(sess, k=args.continueEpoch) validation = True val_acc = [] for epoch in range(args.epochs): if args.continueEpoch is not None and epoch <= args.continueEpoch: continue begin = time.time() sess.run(training_init_op) train_accuracies = [] train_losses = [] for i in range(train_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) if args.adv_flag: _, adv_loss = sess.run([second_train_op, model.adv_loss], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 1 }) _, acc, loss = sess.run( [first_train_op, model.accuracy, model.loss], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 0.5, model.top_k: 5 }) train_accuracies.append(acc) train_losses.append(loss) train_acc_mean = np.mean(train_accuracies[-10:]) train_loss_mean = np.mean(train_losses[-10:]) if (i + 1) % 10 == 0: print( "Epoch %d, Batch %d/%d, time = %ds, train accuracy = %.4f, loss = %.4f " % (epoch, i + 1, train_batches_per_epoch, time.time() - begin, train_acc_mean, train_loss_mean)) train_acc_mean = np.mean(train_accuracies) train_loss_mean = np.mean(train_losses) # print () # compute loss over validation data if validation: sess.run(validation_init_op) val_accuracies = [] for i in range(val_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) acc = sess.run(model.accuracy, feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 1.0, model.top_k: 5 }) val_accuracies.append(acc) val_acc_mean = np.mean(val_accuracies) val_acc.append(val_acc_mean) # log progress to console print("Epoch %d, time = %ds, validation accuracy = %.4f" % (epoch, time.time() - begin, val_acc_mean)) sys.stdout.flush() weights = {} for v in tf.trainable_variables(): weights[v.name] = v.eval() np.save('tuned/weights_' + str(epoch), weights)
import tensorflow as tf from tensorflow.data import Dataset, Iterator dataset_train = Dataset.range(10) dataset_val = Dataset.range(90, 100) iter_train_handle = dataset_train.make_one_shot_iterator().string_handle() iter_val_handle = dataset_val.make_one_shot_iterator().string_handle() handle = tf.placeholder(tf.string, shape=[]) iterator = Iterator.from_string_handle(handle, dataset_train.output_types, dataset_train.output_shapes) next_batch = iterator.get_next() with tf.train.MonitoredTrainingSession() as sess: handle_train, handle_val = sess.run([iter_train_handle, iter_val_handle]) for step in range(10): print('train', sess.run(next_batch, feed_dict={handle: handle_train})) if step % 3 == 0: print('val', sess.run(next_batch, feed_dict={handle: handle_val}))
def load_cifar10_dataset( dataset_dir, batch_size, shuffle, num_workers, drop_last, augment): # load data (x_tr, y_tr), (x_tst, y_tst) = keras.datasets.cifar10.load_data() #n_observations = 100 #x_tr = x_tr[:n_observations] #y_tr = y_tr[:n_observations] #x_tst = x_tst[:n_observations] #y_tst = y_tst[:n_observations] # shape input_shape = list(x_tr.shape) input_shape[0] = None target_shape = list(y_tr.shape) target_shape[0] = None # create placeholders for data input_data_ph = tf.placeholder(tf.float32, shape=input_shape, name='input_data') target_data_ph = tf.placeholder(tf.int64, shape=target_shape, name='target_data') # create placeholder for batch size batch_size_ph = tf.placeholder(tf.int64, name='batch_size') # create data augmentation def augment_images(img, target): img = tf.image.random_flip_left_right(img) img = tf.image.resize_image_with_crop_or_pad(img, 36, 36) img = tf.random_crop(img, [32, 32, 3]) img = tf.cast(img, tf.float32) / 255 return img, target # create cast to float def cast_to_float(img, target): img = tf.cast(img, tf.float32) / 255 return img, target # create the train Dataset object dataset_train = Dataset.from_tensor_slices((input_data_ph, target_data_ph)) if shuffle: dataset_train = dataset_train.shuffle(buffer_size=x_tr.shape[0]) if augment: dataset_train = dataset_train.map(map_func=augment_images, num_parallel_calls=num_workers) else: dataset_train = dataset_train.map(map_func=cast_to_float, num_parallel_calls=num_workers) if drop_last: dataset_train = dataset_train.apply(tf.contrib.data.batch_and_drop_remainder(batch_size_ph)) else: dataset_train = dataset_train.batch(batch_size_ph) dataset_train = dataset_train.prefetch(1) # create the test Dataset object dataset_test = Dataset.from_tensor_slices((input_data_ph, target_data_ph)) dataset_test = dataset_test.map(map_func=cast_to_float, num_parallel_calls=num_workers) dataset_test = dataset_test.batch(batch_size_ph) dataset_test = dataset_test.prefetch(1) # create the reinitializable iterators dataset_output_types = dataset_train.output_types dataset_output_shapes = dataset_train.output_shapes iterator = Iterator.from_structure(dataset_output_types, dataset_output_shapes) iterator_initializer_train = iterator.make_initializer(dataset_train) iterator_initializer_test = iterator.make_initializer(dataset_test) # create the feed_dict for the iterators iterator_feed_dict_train = {input_data_ph: x_tr, target_data_ph: y_tr, batch_size_ph: batch_size} iterator_feed_dict_test = {input_data_ph: x_tst, target_data_ph: y_tst, batch_size_ph: batch_size} # input and target tensors input_tensor, target_tensor = iterator.get_next(name='sample') # dataset dataset_init = [ input_data_ph, target_data_ph, batch_size_ph, input_tensor, target_tensor, iterator_initializer_train, iterator_initializer_test, iterator_feed_dict_train, iterator_feed_dict_test, ] return dataset_init
def train(args): num_classes = 1000 tr_data = ImageDataGenerator( '../data/trainDataPath.txt', dataroot='/media/haohanwang/Info/ImageNet/train/', mode='training', batch_size=args.batch_size, num_classes=num_classes, shuffle=False) val_data = ImageDataGenerator( '../data/valDataPath.txt', dataroot='/media/haohanwang/Info/ImageNet/val/', mode='inference', batch_size=args.batch_size, num_classes=num_classes, shuffle=False) iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) train_batches_per_epoch = int(np.floor(tr_data.data_size / args.batch_size)) val_batches_per_epoch = int(np.floor(val_data.data_size / args.batch_size)) x_re = tf.placeholder(tf.float32, (None, 128 * 128)) x_d = tf.placeholder(tf.float32, (None, 128 * 128)) x = tf.placeholder(tf.float32, (None, 227, 227, 3)) y = tf.placeholder(tf.float32, (None, num_classes)) model = AlexNetHex(x, y, x_re, x_d, args, Hex_flag=True) # model = AlexNet(x, y) optimizer = tf.train.AdamOptimizer(1e-5).minimize(model.loss) saver = tf.train.Saver(tf.trainable_variables()) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('Starting training') print('load Alex net weights') model.load_initial_weights(sess) validation = True val_acc = [] for epoch in range(args.epochs): begin = time.time() sess.run(training_init_op) train_accuracies = [] train_losses = [] for i in range(train_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) batch_xd, batch_re = preparion(img_batch, args) _, acc, loss = sess.run( [optimizer, model.accuracy, model.loss], feed_dict={ x: batch_x, x_re: batch_re, x_d: batch_xd, y: batch_y, model.keep_prob: 0.5, model.top_k: 5 }) train_accuracies.append(acc) train_losses.append(loss) train_acc_mean = np.mean(train_accuracies[-10:]) train_loss_mean = np.mean(train_losses[-10:]) if (i + 1) % 10 == 0: print( "Epoch %d, Batch %d/%d, time = %ds, train accuracy = %.4f, loss = %.4f " % (epoch, i + 1, train_batches_per_epoch, time.time() - begin, train_acc_mean, train_loss_mean)) train_acc_mean = np.mean(train_accuracies) train_loss_mean = np.mean(train_losses) # compute loss over validation data if validation: sess.run(validation_init_op) val_accuracies = [] for i in range(val_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) batch_xd, batch_re = preparion(img_batch, args) acc = sess.run(model.accuracy, feed_dict={ x: batch_x, x_re: batch_re, x_d: batch_xd, y: batch_y, model.keep_prob: 1.0, model.top_k: 5 }) val_accuracies.append(acc) val_acc_mean = np.mean(val_accuracies) val_acc.append(val_acc_mean) # log progress to console print("\nEpoch %d, time = %ds, validation accuracy = %.4f" % (epoch, time.time() - begin, val_acc_mean)) sys.stdout.flush() if (epoch + 1) % 10 == 0: ckpt_file = os.path.join(args.ckpt_dir, 'mnist_model.ckpt') saver.save(sess, ckpt_file) ckpt_file = os.path.join(args.ckpt_dir, 'mnist_model.ckpt') saver.save(sess, ckpt_file) weights = {} for v in tf.trainable_variables(): weights[v.name] = v.eval() np.save('/tuned/weights', weights)
def main(): # 初始参数设置 ratio = 0.3 # 分割训练集和验证集的比例 learning_rate = 1e-3 num_epochs = 17 # 代的个数 之前是10 train_batch_size = 5 # 之前是1024 test_batch_size = 1 dropout_rate = 0.5 num_classes = 4 # 类别标签 display_step = 2 # display_step个train_batch_size训练完了就在tensorboard中写入loss和accuracy # need: display_step <= train_dataset_size / train_batch_size filewriter_path = "./tmp/tensorboard" # 存储tensorboard文件 checkpoint_path = "./tmp/checkpoints" # 训练好的模型和参数存放目录 ckpt_path = "./resnet_v1_50.ckpt" image_format = 'jpg' # ============================================================================ # -----------------生成图片路径和标签的List------------------------------------ file_dir = './dataset' kitchenWaste = [] label_kitchenWaste = [] Recyclables = [] label_Recyclables = [] harmfulGarbage = [] label_harmfulGarbage = [] otherGarbage = [] label_otherGarbage = [] # step1:获取所有的图片路径名,存放到 # 对应的列表中,同时贴上标签,存放到label列表中。 for file in os.listdir(file_dir + '/kitchenWaste'): kitchenWaste.append(file_dir + '/kitchenWaste' + '/' + file) label_kitchenWaste.append(0) for file in os.listdir(file_dir + '/Recyclables'): Recyclables.append(file_dir + '/Recyclables' + '/' + file) label_Recyclables.append(1) for file in os.listdir(file_dir + '/harmfulGarbage'): harmfulGarbage.append(file_dir + '/harmfulGarbage' + '/' + file) label_harmfulGarbage.append(2) for file in os.listdir(file_dir + '/otherGarbage'): otherGarbage.append(file_dir + '/otherGarbage' + '/' + file) label_otherGarbage.append(3) # step2:对生成的图片路径和标签List做打乱处理 image_list = np.hstack( (kitchenWaste, Recyclables, harmfulGarbage, otherGarbage)) label_list = np.hstack((label_kitchenWaste, label_Recyclables, label_harmfulGarbage, label_otherGarbage)) # 利用shuffle打乱顺序 temp = np.array([image_list, label_list]) temp = temp.transpose() np.random.shuffle(temp) # 将所有的img和lab转换成list all_image_list = list(temp[:, 0]) all_label_list = list(temp[:, 1]) # 将所得List分为两部分,一部分用来训练tra,一部分用来测试val # ratio是测试集的比例 n_sample = len(all_label_list) n_val = int(math.ceil(n_sample * ratio)) # 测试样本数 n_train = n_sample - n_val # 训练样本数 tra_images = all_image_list[0:n_train] tra_labels = all_label_list[0:n_train] tra_labels = [int(float(i)) for i in tra_labels] val_images = all_image_list[n_train:-1] val_labels = all_label_list[n_train:-1] val_labels = [int(float(i)) for i in val_labels] # get Datasets # 调用图片生成器,把训练集图片转换成三维数组 train_data = ImageDataGenerator(images=tra_images, labels=tra_labels, batch_size=train_batch_size, num_classes=num_classes, image_format=image_format, shuffle=True) # 调用图片生成器,把测试集图片转换成三维数组 test_data = ImageDataGenerator(images=val_images, labels=val_labels, batch_size=test_batch_size, num_classes=num_classes, image_format=image_format, shuffle=False) # get Iterators with tf.name_scope('input'): # 定义迭代器 train_iterator = Iterator.from_structure(train_data.data.output_types, train_data.data.output_shapes) training_initalizer = train_iterator.make_initializer(train_data.data) test_iterator = Iterator.from_structure(test_data.data.output_types, test_data.data.output_shapes) testing_initalizer = test_iterator.make_initializer(test_data.data) # 定义每次迭代的数据 train_next_batch = train_iterator.get_next() test_next_batch = test_iterator.get_next() x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) with slim.arg_scope(resnet_v1.resnet_arg_scope()): fc8, end_points = resnet_v1.resnet_v1_50(x, num_classes, is_training=True) fc8 = tf.squeeze(fc8, [1, 2]) # loss with tf.name_scope('loss'): loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc8, labels=y)) # optimizer with tf.name_scope('optimizer'): optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # accuracy with tf.name_scope("accuracy"): correct_pred = tf.equal(tf.argmax(fc8, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) init = tf.global_variables_initializer() # Tensorboard tf.summary.scalar('loss', loss_op) tf.summary.scalar('accuracy', accuracy) merged_summary = tf.summary.merge_all() writer = tf.summary.FileWriter(filewriter_path) # load vars flag = False if ckpt_path.split('.')[-2] == '/resnet_v1_50': init = tf.global_variables_initializer() variables = tf.contrib.framework.get_variables_to_restore() variables_to_resotre = [ v for v in variables if v.name.split('/')[1] != 'logits' and v.name.split( '/')[0] != 'optimizer' and v.name.split('/')[0] != 'save' and v.name.split('/')[-1] != 'Adam:0' and v.name.split( '/')[-1] != 'Adam_1:0' and v.name.split('/')[-1] != 'gamma:0' ] # saver = tf.train.Saver() saver = tf.train.Saver(variables_to_resotre) flag = True else: saver = tf.train.Saver() # 定义一代的迭代次数 train_batches_per_epoch = int( np.floor(train_data.data_size / train_batch_size)) test_batches_per_epoch = int( np.floor(test_data.data_size / test_batch_size)) # Start training with tf.Session() as sess: if flag: sess.run(init) saver.restore(sess, ckpt_path) # Tensorboard writer.add_graph(sess.graph) print("{}: Start training...".format(datetime.now())) print("{}: Open Tensorboard at --logdir {}".format( datetime.now(), filewriter_path)) for epoch in range(num_epochs): sess.run(training_initalizer) print("{}: Epoch number: {} start".format(datetime.now(), epoch + 1)) # train for step in range(train_batches_per_epoch): img_batch, label_batch = sess.run(train_next_batch) loss, _ = sess.run([loss_op, train_op], feed_dict={ x: img_batch, y: label_batch, keep_prob: dropout_rate }) if step % display_step == 0: # loss print("{}: loss = {}".format(datetime.now(), loss)) # Tensorboard s = sess.run(merged_summary, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) writer.add_summary(s, epoch * train_batches_per_epoch + step) # accuracy print("{}: Start validation".format(datetime.now())) sess.run(testing_initalizer) test_acc = 0. test_count = 0 for _ in range(test_batches_per_epoch): img_batch, label_batch = sess.run(test_next_batch) acc = sess.run(accuracy, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1.0 }) test_acc += acc test_count += 1 try: test_acc /= test_count except: print('ZeroDivisionError!') print("{}: Validation Accuracy = {:.4f}".format( datetime.now(), test_acc)) # save model print("{}: Saving checkpoint of model...".format(datetime.now())) checkpoint_name = os.path.join( checkpoint_path, 'model_epoch' + str(epoch + 1) + '.ckpt') save_path = saver.save(sess, checkpoint_name) # this epoch is over print("{}: Epoch number: {} end".format(datetime.now(), epoch + 1))
def main(): learning_rate = 1e-3 num_epochs = 10 train_batch_size = 100 test_batch_size = 10 dropout_rate = 0.5 num_classes = 2 display_step = 2 filewriter_path = './model/tensorboard/' checkpoint_path = './model/checkpoints/' image_format = 'jpg' file_name_of_class = ['cat', 'dog'] train_dataset_paths = ['./data/train/cat/', './data/train/dog/'] test_dataset_paths = ['./data/test/cat/', './data/test/dog/'] train_image_pathes = [] train_labels = [] for train_dataset_path in train_dataset_paths: length = len(train_image_pathes) train_image_pathes[length:length] = np.array( glob.glob(train_dataset_path + '*.' + image_format)).tolist() for image_path in train_image_pathes: image_file_name = image_path.split('/')[-1] for i in range(num_classes): if file_name_of_class[i] in image_file_name: train_labels.append(i) break test_image_paths = [] test_labels = [] for test_dataset_path in test_dataset_paths: length = len(test_image_paths) test_image_paths[length:length] = np.array( glob.glob(test_dataset_path + '*.' + image_format)).tolist() for image_path in test_image_paths: iamge_file_name = image_path.split('/')[-1] for i in range(num_classes): if file_name_of_class[i] in image_file_name: test_labels.append(i) break # get Datasets train_data = ImageDataGenerator(images=train_image_pathes, labels=train_labels, batch_size=train_batch_size, num_classes=num_classes, image_format=image_format, shuffle=True) test_data = ImageDataGenerator(images=test_image_paths, labels=test_labels, batch_size=test_batch_size, num_classes=num_classes, image_format=image_format, shuffle=False) # get Iterators with tf.name_scope('input') as scope: train_iterator = Iterator.from_structure(train_data.data.output_types, train_data.data.output_shapes) training_initalizer = train_iterator.make_initializer(train_data.data) test_iterator = Iterator.from_structure(test_data.data.output_types, test_data.data.output_shapes) testing_initalizer = test_iterator.make_initializer(test_data.data) train_next_batch = train_iterator.get_next() test_next_batch = test_iterator.get_next() x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) # alexnet fc8 = alexnet(x, keep_prob, num_classes) # loss with tf.name_scope('loss') as scope: loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc8, labels=y)) # optimizer with tf.name_scope('optimizer') as scope: optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # accuracy with tf.name_scope('accuracy') as scope: correct_pred = tf.equal(tf.argmax(fc8, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) init = tf.global_variables_initializer() tf.summary.scalar('loss', loss_op) tf.summary.scalar('accuracy', accuracy) merged_summary = tf.summary.merge_all() writer = tf.summary.FileWriter(filewriter_path) saver = tf.train.Saver() train_batches_per_epoch = int( np.floor(train_data.data_size / train_batch_size)) test_batches_per_epoch = int( np.floor(test_data.data_size / test_batch_size)) with tf.Session() as sess: sess.run(init) writer.add_graph(sess.graph) print('{}: Start training ...'.format(datetime.now())) print('{}. Open Tensorboard at --logdir {}'.format( datetime.now(), filewriter_path)) for epoch in range(num_epochs): sess.run(training_initalizer) print('{}: Epoch number: {} start'.format(datetime.now(), epoch + 1)) for step in range(train_batches_per_epoch): img_batch, label_batch = sess.run(train_next_batch) print('==========================>', img_batch.shape) print('==========================>', label_batch.shape) loss, _ = sess.run([loss_op, train_op], feed_dict={ x: img_batch, y: label_batch, keep_prob: dropout_rate }) if step % display_step == 0: print('{}: loss = {}'.format(datetime.now(), loss)) s = sess.run(merged_summary, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1 }) writer.add_summary(s, epoch * train_batches_per_epoch + step) # accuracy print('{}: Start validation'.format(datetime.now())) sess.run(testing_initalizer) test_acc = 0 test_count = 0 for _ in range(test_batches_per_epoch): img_batch, label_batch = sess.run(test_next_batch) acc = sess.run(accuracy, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1 }) test_acc += acc test_count += 1 try: test_acc /= test_count except: print('ZeroDivisionError!') print('{}: Validation accuracy = {:.4f}'.format( datetime.now(), test_acc)) # save model print('{}: Saving checkpoints of model ... '.format( datetime.now())) checkpoint_name = os.path.join( checkpoint_path, 'model_epoch' + str(epoch + 1) + '.ckpt') save_path = saver.save(sess, checkpoint_name) # this epoch is over print('{}: Epoch number: {} end'.format(datetime.now(), epoch + 1))
def print_files(self): for x, y in zip(self.imgs_files, self.labels_files): print(x, y) if __name__ == "__main__": config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config) with tf.device('/cpu:0'): #segdl= SegDataLoader('/home/eren/Data/Cityscapes/', 10, (512,1024), (512,512), 'train.txt') segdl = SegDataLoader('/home/eren/Data/Cityscapes/', 10, (512, 1024), (512, 512), 'val.txt', split='val') iterator = Iterator.from_structure(segdl.data_tr.output_types, segdl.data_tr.output_shapes) next_batch = iterator.get_next() training_init_op = iterator.make_initializer(segdl.data_tr) session.run(training_init_op) for i in range(10): img_batch, label_batch = session.run(next_batch) # print(img_batch) # img_batch= np.asarray(img_batch,dtype=np.uint8) # plt.imshow(label_batch[0,0,:,:,0]);plt.show() # plt.imshow(img_batch[0,0,:,:,:]);plt.show()
# Place data loading and preprocessing on the cpu with tf.device('/cpu:0'): tr_data = ImageDataGenerator(train_file, mode='training', batch_size=batch_size, num_classes=num_classes, shuffle=False) val_data = ImageDataGenerator(val_file, mode='inference', batch_size=batch_size, num_classes=num_classes, shuffle=False) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) # TF placeholder for graph input and output x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3]) y = tf.placeholder(tf.float32, [batch_size, num_classes]) keep_prob = tf.placeholder(tf.float32) # Initialize model model = AlexNet(x, keep_prob, num_classes, train_layers) # Link variable to model output
batch_size = 1 # todo: num_classes = 2 # Place data loading and preprocessing on the cpu with tf.device('/cpu:0'): pre_data = PreDataGenerator(pre_file, mode='predicting', batch_size=batch_size, num_classes=num_classes, iterator_size=FLAGS.pre_size, kth_init_op=FLAGS.iter_epoch, classifier_version=4, ) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(pre_data.data.output_types, pre_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators predicting_init_op = iterator.make_initializer(pre_data.data) x = tf.placeholder(tf.float32, [batch_size, 227, 227, 6]) keep_prob = tf.constant(1., dtype=tf.float32) # Initialize model model = AlexNet(x, keep_prob, 2, []) # Link variable to model output score = model.fc8 softmax = tf.nn.softmax(score)
def _load_data(self): # Read txt files containing infos about the dataset with open(DB_PATH + 'classes.txt', 'r') as fi: for line in fi: items = line.split(', ') self._class_id2name_map[int(items[0])] = items[1].rstrip() is_train_list = list() with open(DB_PATH + 'train_test_split.txt', 'r') as fi: for line in fi: items = line.split(', ') is_train_list.append(int(items[1])) image_list = list() with open(DB_PATH + 'images.txt', 'r') as fi: for line in fi: items = line.split(', ') image_list.append('images/' + items[1][:-1]) class_lables = list() with open(DB_PATH + 'image_class_labels.txt', 'r') as fi: for line in fi: items = line.split(' ') class_lables.append(int(items[1]) - 1) # check inputs assert len(is_train_list) == len( image_list), "Lengths dont match. Input data seems broken." assert len(is_train_list) == len( class_lables), "Lengths dont match. Input data seems broken." # shuffle lists ind = list(range(len(is_train_list))) shuffle(ind) is_train_list = [is_train_list[i] for i in ind] image_list = [image_list[i] for i in ind] class_lables = [class_lables[i] for i in ind] # get samples of the set we want for is_train, class_lable, file_path in zip(is_train_list, class_lables, image_list): if self._is_train and (is_train == 1): self._img_list.append(file_path) self._lable_list.append(class_lable) elif (not self._is_train) and (is_train == 0): self._img_list.append(file_path) self._lable_list.append(class_lable) self.num_samples = len(self._img_list) # turn into tensorflow objects dataset = Dataset.from_tensor_slices( (self._img_list, self._lable_list)) # load images and turn into one hot encoding if self._is_train: dataset = dataset.map(self._input_parser_train, num_parallel_calls=4) else: dataset = dataset.map(self._input_parser_test, num_parallel_calls=4) # shuffle dataset = dataset.shuffle(buffer_size=32) # repeat indefinitely dataset = dataset.repeat(-1) # batch the data dataset = dataset.batch(self._batch_size) # shared iterator iterator = Iterator.from_structure(dataset.output_types, dataset.output_shapes) # create two initialization ops to switch between the datasets self._init_op = iterator.make_initializer(dataset) self.get_data = iterator.get_next()
def run_net(args, test): # Path for tf.summary.FileWriter and to store model checkpoints print(args) r = round(np.random.randn(), 4) # Create parent path if it doesn't exist if not os.path.isdir(args['checkpoint_path']): os.makedirs(args['checkpoint_path']) # data tr_data = ImageDataGenerator(args['train_file'], mode='training', batch_size=args['batch_size'], num_classes=args['num_classes'], img_size=args['img_size'], shuffle=True) val_data = ImageDataGenerator(args['val_file'], mode='inference', batch_size=args['batch_size'], img_size=args['img_size'], num_classes=args['num_classes'], shuffle=False) if args['v'] != 'None': v_data = ImageDataGeneratorV(args['v_file'], mode='inference', batch_size=args['v_batch_size']) iterator_v1 = Iterator.from_structure(v_data.data.output_types, v_data.data.output_shapes) next_batch_v1 = iterator_v1.get_next() v_init_op = iterator_v1.make_initializer(v_data.data) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) # TF placeholder for graph input and output if args['v'] != 'None': img1 = tf.placeholder(tf.float32, [args['v_batch_size'], 227, 227, 3]) img2 = tf.placeholder(tf.float32, [args['v_batch_size'], 227, 227, 3]) RSA = tf.placeholder(tf.float32, [args['v_batch_size']]) lam = tf.placeholder(tf.float32) x = tf.placeholder( tf.float32, [args['batch_size'], args['img_size'], args['img_size'], 3]) y = tf.placeholder(tf.float32, [args['batch_size'], args['num_classes']]) keep_prob = tf.placeholder(tf.float32) # Initialize model model = CORNetZV(x, keep_prob, args['num_classes'], args['train_layers']) # Link variable to model output if (args['v'] == 'V1'): pool_tf1 = model.forward_V1(img1) pool_tf2 = model.forward_V1(img2) score = model.forward() # Op for calculating the loss with tf.name_scope("cross_ent"): cif_cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=score, labels=y)) if args['v'] != 'None': v_cost = compute_cost_V(pool_tf1, pool_tf2, RSA) total_cost = get_total_cost(v_cost, cif_cost, lam) # Train op # Create optimizer and apply gradient descent to the trainable variables optimizer = tf.train.GradientDescentOptimizer(args['learning_rate']) train_op = optimizer.minimize(loss=cif_cost, global_step=tf.train.get_global_step()) if args['v'] != 'None': train_op_2 = optimizer.minimize(loss=total_cost, global_step=tf.train.get_global_step()) # Evaluation op: Accuracy of the model model_pred = tf.argmax(score, 1) act_pred = tf.argmax(y, 1) correct_pred = tf.equal(model_pred, act_pred) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # Initialize an saver for store model checkpoints saver = tf.train.Saver() # Get the number of training/validation steps per epoch train_batches_per_epoch = int( np.floor(tr_data.data_size / args['batch_size'])) val_batches_per_epoch = int( np.floor(val_data.data_size / args['batch_size'])) if (test): print('Running test with 5 batches per epoch') train_batches_per_epoch = 10 val_batches_per_epoch = 10 #config = tf.ConfigProto(log_device_placement=True) config = tf.ConfigProto() config.gpu_options.allow_growth = True if (args['v'] != 'None'): f_info = 'CORNetZ_' + str(args['train_file']) + str( args['v']) + 'ratio=' + str(args['v_ratio']) + 'num_epochs=' + str( args['num_epochs']) + 'n_e_c=' + str( args['n_e_c']) + 'n_e_v=' + str( args['n_e_v']) + '_' + str(r) df = pd.DataFrame(np.zeros((args['num_epochs'], 8)), columns=[ 'train_fine_acc', 'train_coarse_acc', 'test_fine_acc', 'test_coarse_acc', 'cif_cost_train', 'cif_cost_test', 'v_cost', 'time' ]) else: f_info = 'CORNetZ_' + str(args['train_file']) + str( args['num_epochs']) + '_' + str(r) df = pd.DataFrame(np.zeros((args['num_epochs'], 7)), columns=[ 'train_fine_acc', 'train_coarse_acc', 'test_fine_acc', 'test_coarse_acc', 'cif_cost_train', 'cif_cost_test', 'time' ]) results_f = args['results_path'] + f_info + '.csv' t0 = time.time() if args['v'] != 'None': print('Running ' + str(args['v']) + ' ratio = ' + str(args['v_ratio']) + ' train file ' + str(args['train_file'])) else: print('Running CORNetZ' + ' train file ' + str(args['train_file'])) #f.write(str(ratio) + '\n') with tf.Session(config=config) as sess: # Initialize all variables sess.run(tf.global_variables_initializer()) #saver.restore(sess, 'log/checkpoints/CORNETZmodel_epoch5.ckpt') print("{} Start training...".format(datetime.now())) # Loop over number of epochs for epoch in range(args['num_epochs']): print("{} Epoch number: {}".format(datetime.now(), epoch + 1)) # Initialize iterator with the training dataset sess.run(training_init_op) if (args['v'] != 'None'): sess.run(v_init_op) train_fine_acc = 0. train_count = 0 train_coarse_acc = 0 cif_cost_train = 0 v_cost_ = 0 for step in range(train_batches_per_epoch): # get next batch of data t = which_train(step, args, epoch) img_batch, label_batch = sess.run(next_batch) cif_cost_cur = sess.run(cif_cost, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) cif_cost_train += cif_cost_cur acc, model_preds, act_preds = sess.run( [accuracy, model_pred, act_pred], feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) c_acc = get_coarse_accuracy(model_preds, act_preds) train_coarse_acc += c_acc train_fine_acc += acc train_count += 1 if (args['v'] != 'None'): # get v1 batches img1_batch, img2_batch, RSAs_batch = sess.run( next_batch_v1) ## calculate costs for lambda vlaue v_cost_cur = sess.run(v_cost, feed_dict={ img1: img1_batch, img2: img2_batch, RSA: RSAs_batch }) v_cost_ += v_cost_cur if (t == 'CE'): sess.run(train_op, feed_dict={ x: img_batch, y: label_batch, keep_prob: args['dropout_rate'] }) else: lam_cur = (float(args['v_ratio']) * cif_cost_cur) / (v_cost_cur) ## run v1 training op on total cost sess.run(train_op_2, feed_dict={ img1: img1_batch, img2: img2_batch, RSA: RSAs_batch, lam: lam_cur, x: img_batch, y: label_batch, keep_prob: args['dropout_rate'] }) train_fine_acc /= train_count train_coarse_acc /= train_count cif_cost_train /= train_count if (v_cost_ != 0): v_cost_ /= train_count # Validate the model on the entire validation set print("{} Start validation for epoch= " + str(epoch + 1) + ' ' + format(datetime.now())) sess.run(validation_init_op) test_fine_acc = 0. test_count = 0 test_coarse_acc = 0 cif_cost_test = 0 for _ in range(val_batches_per_epoch): img_batch, label_batch = sess.run(next_batch) acc, model_preds, act_preds = sess.run( [accuracy, model_pred, act_pred], feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) cif_cost_cur = sess.run(cif_cost, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) cif_cost_test += cif_cost_cur c_acc = get_coarse_accuracy(model_preds, act_preds) test_coarse_acc += c_acc test_fine_acc += acc test_count += 1 test_fine_acc /= test_count test_coarse_acc /= test_count cif_cost_test /= test_count ti = time.time() time_run = (ti - t0) / 60 df['train_fine_acc'].ix[epoch] = train_fine_acc df['train_coarse_acc'].ix[epoch] = train_coarse_acc df['test_fine_acc'].ix[epoch] = test_fine_acc df['test_coarse_acc'].ix[epoch] = test_coarse_acc df['cif_cost_train'].ix[epoch] = cif_cost_train df['cif_cost_test'].ix[epoch] = cif_cost_test if args['v'] != 'None': df['v_cost'].ix[epoch] = v_cost_ df['time'].ix[epoch] = time_run df.to_csv(results_f) print("Time to run epoch " + str(epoch + 1) + ' : ' + str(round(time_run, 2)) + ' minutes') print("{} Validation Accuracy = {:.4f}".format( datetime.now(), test_fine_acc)) print("{} Validation Coarse Accuracy = {:.4f}".format( datetime.now(), test_coarse_acc)) print("{} Training Accuracy = {:.4f}".format( datetime.now(), train_fine_acc)) print("{} Training Coarse Accuracy = {:.4f}".format( datetime.now(), train_coarse_acc)) print("{} Validation Cost= {:.4f}".format(datetime.now(), cif_cost_test)) print("{} Training Cost = {:.4f}".format(datetime.now(), cif_cost_train)) # save checkpoint of the model checkpoint_name = os.path.join( args['checkpoint_path'], 'weights' + f_info + '_epoch' + str(epoch + 1) + '.ckpt') #if not args['test']: save_path = saver.save(sess, checkpoint_name) print("{} Model checkpoint saved at {}".format( datetime.now(), checkpoint_name))
def test_generator(config_file): config = parse_config(config_file) config_data = config['dataset'] temp_dir = config_data['temp_dir'] batch_size = config['sampler']['batch_size'] # Place data loading and preprocessing on the cpu tf.set_random_seed(0) with tf.device('/cpu:0'): tr_data = ImageDataGenerator(config_data['data_train'], config['sampler']) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators training_init_op = iterator.make_initializer(tr_data.data) #validation_init_op = iterator.make_initializer(val_data.data) train_batches_per_epoch = config['training']['batch_number'] num_epochs = config['training']['maximal_epoch'] app_type = config['training']['app_type'] # Start Tensorflow session with tf.Session() as sess: # Initialize all variables sess.run(tf.global_variables_initializer()) total_step = 0 # Loop over number of epochs for epoch in range(num_epochs): print("{} Epoch number: {}".format(datetime.now(), epoch + 1)) # Initialize iterator with the training dataset sess.run(training_init_op) for step in range(train_batches_per_epoch): if (app_type == 0): [img_batch, weight_batch, label_batch] = sess.run(next_batch) img_0 = img_batch[0, :, :, :, :] lab_0 = label_batch[0, :, :, :, 0] print(epoch, step, img_0.shape, lab_0.shape) if (config['training']['save_postfix'] == 'nii.gz'): img_0 = img_0[:, :, :, 0] img_d = img_0.shape[0] lab_d = lab_0.shape[0] if (lab_d < img_d): margin = (img_d - lab_d) / 2 pad_lab = np.zeros_like(img_0) pad_lab[np.ix_(range(margin, margin + lab_d), range(lab_0.shape[1]), range(lab_0.shape[2]))] = lab_0 lab_0 = pad_lab save_array_as_nifty_volume( img_0, '{0:}/img{1:}.nii.gz'.format(temp_dir, total_step)) save_array_as_nifty_volume( lab_0, '{0:}/lab{1:}.nii.gz'.format(temp_dir, total_step)) elif (config['training']['save_postfix'] == 'jpg'): iten_mean = np.asarray( [179.69427237, 146.44891944, 134.39686832]) iten_std = np.asarray( [40.37515566, 42.92464467, 46.74197245]) img_0 = img_0[0] * iten_std + iten_mean img_0 = np.asarray(img_0, np.uint8) lab_0 = np.asarray(lab_0[0], np.uint8) * 255 print(img_0.min(), img_0.max(), lab_0.min(), lab_0.max()) img_0 = Image.fromarray(img_0, 'RGB') lab_0 = Image.fromarray(lab_0, 'L') img_0.save('{0:}/img{1:}.jpg'.format( temp_dir, total_step)) lab_0.save('{0:}/lab{1:}.jpg'.format( temp_dir, total_step)) else: [img_batch, stp] = sess.run(next_batch) print(stp) img_0 = img_batch[0, 0, :, :, 0] plt.imshow(img_0) plt.show() total_step = total_step + 1
""" Main Part of the finetuning Script. """ # Create parent path if it doesn't exist # Place data loading and preprocessing on the cpu with tf.device('/cpu:0'): val_data = ImageDataGenerator(image_path, img_size, val_file, mode='inference', batch_size=batch_size, num_classes=num_classes, shuffle=False) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(val_data.data.output_types, val_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators validation_init_op = iterator.make_initializer(val_data.data) # Get the number of training/validation steps per epoch # val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size)) val_batches_per_epoch = 1 fm_path = os.path.join('feature_map', task_name) if not os.path.isdir(fm_path): os.makedirs(fm_path) # Start Tensorflow session with tf.Session() as sess:
def train(args): num_classes = 7 dataroot = '../data/PACS/' batch_size = args.batch_size train_file, val_file, test_file = set_path(args.cat) tr_data = ImageDataGenerator(train_file, dataroot=dataroot, mode='training', batch_size=batch_size, num_classes=num_classes, shuffle=True) val_data = ImageDataGenerator(val_file, dataroot=dataroot, mode='inference', batch_size=batch_size, num_classes=num_classes, shuffle=False) test_data = ImageDataGenerator(test_file, dataroot=dataroot, mode='inference', batch_size=batch_size, num_classes=num_classes, shuffle=False) iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) test_init_op = iterator.make_initializer(test_data.data) train_batches_per_epoch = int(np.floor(tr_data.data_size / args.batch_size)) val_batches_per_epoch = int(np.floor(val_data.data_size / args.batch_size)) test_batches_per_epoch = int( np.floor(test_data.data_size / args.batch_size)) x = tf.placeholder(tf.float32, (None, 227, 227, 3)) y = tf.placeholder(tf.float32, (None, num_classes)) model = AlexNet(x, y, args) optimizer1 = tf.train.AdamOptimizer(1e-5) #1e-5 for art/sketch first_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "cnn") first_train_op = optimizer1.minimize(model.loss, var_list=first_train_vars) if args.adv_flag: optimizer2 = tf.train.AdamOptimizer(1e-3) second_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "adv") second_train_op = optimizer2.minimize(model.adv_loss, var_list=second_train_vars) saver = tf.train.Saver(tf.trainable_variables()) with tf.Session() as sess: print('Starting training') print('load Alex net weights') sess.run(tf.initialize_all_variables()) model.load_initial_weights(sess) if args.load_params: ckpt_file = os.path.join(args.ckpt_dir, 'mnist_model.ckpt') print('Restoring parameters from', ckpt_file) saver.restore(sess, ckpt_file) validation = True best_validate_accuracy = 0 score = 0 train_acc = [] test_acc = [] val_acc = [] for epoch in range(args.epochs): begin = time.time() sess.run(training_init_op) train_accuracies = [] train_losses = [] # if args.adv_flag: for i in range(train_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) if args.adv_flag: _, adv_loss = sess.run([second_train_op, model.adv_loss], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 1.0 }) _, acc, loss = sess.run( [first_train_op, model.accuracy, model.loss], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 0.5 }) train_accuracies.append(acc) train_losses.append(loss) train_acc_mean = np.mean(train_accuracies) train_acc.append(train_acc_mean) train_loss_mean = np.mean(train_losses) # print () # compute loss over validation data if validation: sess.run(validation_init_op) val_accuracies = [] for i in range(val_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) acc = sess.run([model.accuracy], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 1.0 }) val_accuracies.append(acc) val_acc_mean = np.mean(val_accuracies) val_acc.append(val_acc_mean) # log progress to console print( "Epoch %d, time = %ds, train accuracy = %.4f, loss = %.4f, validation accuracy = %.4f" % (epoch, time.time() - begin, train_acc_mean, train_loss_mean, val_acc_mean)) if val_acc_mean > best_validate_accuracy: best_validate_accuracy = val_acc_mean test_accuracies = [] sess.run(test_init_op) for i in range(test_batches_per_epoch): batch_x, img_batch, batch_y = sess.run(next_batch) acc = sess.run([model.accuracy], feed_dict={ x: batch_x, y: batch_y, model.keep_prob: 1.0 }) test_accuracies.append(acc) score = np.mean(test_accuracies) print("Best Validated Model Prediction Accuracy = %.4f " % (score)) sys.stdout.flush() test_acc.append(score) print("Best Validated Model Prediction Accuracy = %.4f " % (score)) return (train_acc, val_acc, test_acc)
def dataInputGen(): # File Path desc train_file = "./TrainingFile_detail.txt" Validate_file = "./ValidationFile_detail.txt" class_file = "./Classmap.txt" checkpoint_path = "./ckp/" # Network params learning_rate = 0.00001 dropout_rate = 0.5 num_classes = 1000 batch_size = 1 num_epochs = 100 display_step = 20 #check acc per epoach train_layers = ['fc8', 'fc7', 'fc6'] #read all image path config train_img_paths,train_labels = read_train_detail(train_file) validate_img_paths,validate_labels = read_train_detail(Validate_file) print("Total Dataset {}".format(int(len(train_labels)+len(validate_labels)))) print("Split to Training {} and to Validation {}".format(len(train_labels),len(validate_labels))) with tf.device('/cpu:0'): tr_data = ImageDataGenerator(mode='training', batch_size=batch_size, num_classes=num_classes, class_file=class_file, shuffle=False, img_paths=train_img_paths, labels=train_labels) # create an reinitializable iterator given the dataset structure iterator = Iterator.from_structure(tr_data.data.output_types, tr_data.data.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators training_init_op = iterator.make_initializer(tr_data.data) validation_init_op = iterator.make_initializer(val_data.data) # Get the number of training/validation steps per epoch train_batches_per_epoch = int(np.floor(tr_data.data_size/batch_size)) val_batches_per_epoch = int(np.floor(val_data.data_size/batch_size)) # TF placeholder for graph input and output x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) # Initialize model model = AlexNet(x, keep_prob, num_classes, train_layers,weights_path="./weights/bvlc_alexnet.npy") # Link variable to model output score = model.fc8 # List of trainable variables of the layers we want to train var_list = [v for v in tf.trainable_variables() if v.name.split('/')[0] in train_layers] # Op for calculating the loss with tf.name_scope("cross_ent"): loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=score,labels=y)) # Train op with tf.name_scope("train"): optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss) # Evaluation op: Accuracy of the model with tf.name_scope("accuracy"): correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32),name="ACC") # Add the loss to summary tf.summary.scalar('cross_entropy', loss) tf.summary.scalar('Acc', accuracy) # Merge all summaries together merged_summary = tf.summary.merge_all() # Initialize the FileWriter writer = tf.summary.FileWriter("./Graph") # Initialize an saver for store model checkpoints saver = tf.train.Saver() with tf.Session() as sess: # Initialize all variables sess.run(tf.global_variables_initializer()) # Add the model graph to TensorBoard writer.add_graph(sess.graph) # Load the pretrained weights into the non-trainable layer print("Weight Loading...") model.load_initial_weights(sess) print("{} Start training...".format(datetime.now())) best_acc = 0 # Loop over number of epochs for epoch in range(num_epochs): print("{} Epoch number: {}".format(datetime.now(), epoch+1)) # Initialize iterator with the training dataset sess.run(training_init_op) for step in tqdm(range(train_batches_per_epoch)): # get next batch of data img_batch, label_batch = sess.run(next_batch) # And run the training op sess.run(train_op, feed_dict={x: img_batch, y: label_batch, keep_prob: dropout_rate}) # Generate summary with the current batch of data and write to file if step % display_step == 0: s,acc_data = sess.run([merged_summary,accuracy], feed_dict={x: img_batch, y: label_batch, keep_prob: 1.}) writer.add_summary(s, epoch*train_batches_per_epoch + step) #print("Accuracy at steps {} is {:f}".format((epoch*train_batches_per_epoch + step),acc_data)) # Validate the model on the entire validation set print("{} Start validation".format(datetime.now())) sess.run(validation_init_op) test_acc = 0. test_count = 0 for _ in tqdm(range(val_batches_per_epoch)): img_batch, label_batch = sess.run(next_batch) acc = sess.run(accuracy, feed_dict={x: img_batch, y: label_batch, keep_prob: 1.}) test_acc += acc test_count += 1 test_acc /= test_count print("{} Validation Accuracy = {:.4f}".format(datetime.now(), test_acc)) print("Checking improving Accuracy...") if test_acc > best_acc: print("Accuracy improve from {:.4f} to {:.4f}".format(best_acc,test_acc)) print("Saving best weights ...") save_path = saver.save(sess, "./BestWeight/weight") best_acc = test_acc else: print("Accuracy not improve") print("{} Saving checkpoint of model...".format(datetime.now())) # save checkpoint of the model checkpoint_name = os.path.join(checkpoint_path, 'model_epoch'+str(epoch+1)+'.ckpt') save_path = saver.save(sess, checkpoint_name) print("{} Model checkpoint saved at {}".format(datetime.now(), checkpoint_name))
def main(): # 初始参数设置 learning_rate = 1e-3 num_epochs = 17 # 代的个数 之前是10 train_batch_size = 1000 # 之前是1024 test_batch_size = 100 dropout_rate = 0.5 num_classes = 2 # 类别标签 display_step = 2 # display_step个train_batch_size训练完了就在tensorboard中写入loss和accuracy # need: display_step <= train_dataset_size / train_batch_size filewriter_path = "./tmp/tensorboard" # 存储tensorboard文件 checkpoint_path = "./tmp/checkpoints" # 训练好的模型和参数存放目录 image_format = 'jpg' # 数据集的数据类型 file_name_of_class = ['cat', 'dog'] # cat对应标签0,dog对应标签1。默认图片包含独特的名词,比如类别 train_dataset_paths = [ 'G:/Lab/Data_sets/catanddog/train/cat/', 'G:/Lab/Data_sets/catanddog/train/dog/' ] # 指定训练集数据路径(根据实际情况指定训练数据集的路径) test_dataset_paths = [ 'G:/Lab/Data_sets/catanddog/test/cat/', 'G:/Lab/Data_sets/catanddog/test/dog/' ] # 指定测试集数据路径(根据实际情况指定测试数据集的路径) # 注意:默认数据集中的样本文件名称中包含其所属类别标签的名称,即file_name_of_class中的名称 # 初始参数设置完毕 # 训练数据集数据处理 train_image_paths = [] train_labels = [] # 打开训练数据集目录,读取全部图片,生成图片路径列表 for train_dataset_path in train_dataset_paths: length = len(train_image_paths) train_image_paths[length:length] = np.array( glob.glob(train_dataset_path + '*.' + image_format)).tolist() for image_path in train_image_paths: image_file_name = image_path.split('/')[-1] for i in range(num_classes): if file_name_of_class[i] in image_file_name: train_labels.append(i) break # 测试数据集数据处理 test_image_paths = [] test_labels = [] # 打开测试数据集目录,读取全部图片,生成图片路径列表 for test_dataset_path in test_dataset_paths: length = len(test_image_paths) test_image_paths[length:length] = np.array( glob.glob(test_dataset_path + '*.' + image_format)).tolist() for image_path in test_image_paths: image_file_name = image_path.split('/')[-1] for i in range(num_classes): if file_name_of_class[i] in image_file_name: test_labels.append(i) break # get Datasets # 调用图片生成器,把训练集图片转换成三维数组 train_data = ImageDataGenerator(images=train_image_paths, labels=train_labels, batch_size=train_batch_size, num_classes=num_classes, image_format=image_format, shuffle=True) # 调用图片生成器,把测试集图片转换成三维数组 test_data = ImageDataGenerator(images=test_image_paths, labels=test_labels, batch_size=test_batch_size, num_classes=num_classes, image_format=image_format, shuffle=False) # get Iterators with tf.name_scope('input'): # 定义迭代器 train_iterator = Iterator.from_structure(train_data.data.output_types, train_data.data.output_shapes) training_initalizer = train_iterator.make_initializer(train_data.data) test_iterator = Iterator.from_structure(test_data.data.output_types, test_data.data.output_shapes) testing_initalizer = test_iterator.make_initializer(test_data.data) # 定义每次迭代的数据 train_next_batch = train_iterator.get_next() test_next_batch = test_iterator.get_next() x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) # alexnet fc8 = alexnet(x, keep_prob, num_classes) # loss with tf.name_scope('loss'): loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc8, labels=y)) # optimizer with tf.name_scope('optimizer'): optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # accuracy with tf.name_scope("accuracy"): correct_pred = tf.equal(tf.argmax(fc8, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) init = tf.global_variables_initializer() # Tensorboard tf.summary.scalar('loss', loss_op) tf.summary.scalar('accuracy', accuracy) merged_summary = tf.summary.merge_all() writer = tf.summary.FileWriter(filewriter_path) # saver saver = tf.train.Saver() # 定义一代的迭代次数 train_batches_per_epoch = int( np.floor(train_data.data_size / train_batch_size)) test_batches_per_epoch = int( np.floor(test_data.data_size / test_batch_size)) # Start training with tf.Session() as sess: sess.run(init) #saver.restore(sess, "./tmp/checkpoints/model_epoch18.ckpt") # Tensorboard writer.add_graph(sess.graph) print("{}: Start training...".format(datetime.now())) print("{}: Open Tensorboard at --logdir {}".format( datetime.now(), filewriter_path)) for epoch in range(num_epochs): sess.run(training_initalizer) print("{}: Epoch number: {} start".format(datetime.now(), epoch + 1)) # train for step in range(train_batches_per_epoch): img_batch, label_batch = sess.run(train_next_batch) loss, _ = sess.run([loss_op, train_op], feed_dict={ x: img_batch, y: label_batch, keep_prob: dropout_rate }) if step % display_step == 0: # loss print("{}: loss = {}".format(datetime.now(), loss)) # Tensorboard s = sess.run(merged_summary, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1. }) writer.add_summary(s, epoch * train_batches_per_epoch + step) # accuracy print("{}: Start validation".format(datetime.now())) sess.run(testing_initalizer) test_acc = 0. test_count = 0 for _ in range(test_batches_per_epoch): img_batch, label_batch = sess.run(test_next_batch) acc = sess.run(accuracy, feed_dict={ x: img_batch, y: label_batch, keep_prob: 1.0 }) test_acc += acc test_count += 1 try: test_acc /= test_count except: print('ZeroDivisionError!') print("{}: Validation Accuracy = {:.4f}".format( datetime.now(), test_acc)) # save model print("{}: Saving checkpoint of model...".format(datetime.now())) checkpoint_name = os.path.join( checkpoint_path, 'model_epoch' + str(epoch + 1) + '.ckpt') save_path = saver.save(sess, checkpoint_name) # this epoch is over print("{}: Epoch number: {} end".format(datetime.now(), epoch + 1))
def training(): batch_size = 10 noise_type = 'gaussian' noise_proportion = 0.2 noise_mean = 0 noise_std = 1 noise_lam = 1 noise_std_range = [1,5] noise_lam_range = [1,5] loss_type = 'l2_loss' study_rate = 1e-5 current_dir = Path('.') train_true,__,test_true=load_data(dataset='mias', DIR=current_dir) train_true = train_true.repeat().batch(batch_size) train_true = train_true.prefetch(buffer_size = tf.data.experimental.AUTOTUNE) test_true = test_true.repeat().batch(batch_size) test_true = test_true.prefetch(buffer_size = tf.data.experimental.AUTOTUNE) iter_train_handle = train_true.make_one_shot_iterator().string_handle() iter_val_handle = test_true.make_one_shot_iterator().string_handle() handle = tf.placeholder(tf.string, shape=[]) iterator = Iterator.from_string_handle(handle, train_true.output_types, train_true.output_shapes) next_batch = iterator.get_next() noise_args = {'proportion':noise_proportion} if noise_type == 'random': noise_fn = random_noise noise_args['std_range'] = noise_std_range noise_args['lam_range'] = noise_lam_range elif noise_type == 'poisson': noise_fn = poisson_noise noise_args['lam'] = noise_lam else: noise_fn = gaussian_noise noise_args['mean'] = noise_mean noise_args['std'] = noise_std true_img = tf.placeholder(tf.uint8, shape=[batch_size, 64, 64, 1]) noised_img = noise_fn(**noise_args, image=true_img) model_input = tf.cast(noised_img, dtype=tf.float32) denoised_img = QAE.build_QAE(model_input) if loss_type == 'l2_loss': train_loss = l2_loss( tf.cast(true_img, dtype=tf.float32), denoised_img) # val_loss = l2_loss( # tf.cast(test_true_img, # dtype=tf.float32), # test_denoised_img) total_train_loss = train_loss optimizer = tf.train.AdamOptimizer(\ learning_rate=study_rate).minimize( total_train_loss) tf.summary.scalar('train_l2_loss',train_loss) # tf.summary.scalar('val_l2_loss',val_loss) tf.summary.scalar('total_train_loss', total_train_loss) merged_summary = tf.summary.merge_all() train_writer = tf.summary.FileWriter( current_dir/'train_data') init_vars = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() with tf.Session().as_default() as sess: global_step = tf.train.get_global_step() handle_train, handle_val = sess.run( [iter_train_handle, iter_val_handle]) sess.run(init_vars) for step in range(500): train_true_img = sess.run(next_batch, feed_dict={handle: handle_train}) test_true_img = sess.run(next_batch, feed_dict={handle: handle_val}) _ = sess.run(optimizer, feed_dict={true_img:train_true_img}) t_summ = sess.run(merged_summary, feed_dict={true_img:train_true_img}) t_loss = sess.run(total_train_loss, feed_dict={true_img:train_true_img}) train_writer.add_summary(t_summ,step) print('Iter:{}, Training Loss {}'.format( step, t_loss)) if step%20 == 0: fig,axes = print('done')