def get_kaggle_reader(validation_pct=.2): train_file = Path('./train.zip') test_file = Path('./test.zip') if not train_file.is_file() or not test_file.is_file(): print('👇👇👇👇👇👇👇👇👇👇👇👇👇') print( 'Please put train.zip and test.zip from ' 'https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition to project root path.' ) raise FileNotFoundError() train_arc = ZipTarReader.auto(train_file.name) test_arc = ZipTarReader.auto(test_file.name) file_list = train_arc.shuffled_namelist() file_list = np.array(file_list) mid = int(len(file_list) * validation_pct) train_file_list = file_list[mid:] vali_file_list = file_list[:mid] return BatchReader.BatchReader(train_arc, train_file_list, KaggleLabelClassifier),\ BatchReader.BatchReader(train_arc, vali_file_list, KaggleLabelClassifier),\ BatchReader.BatchReader(test_arc, test_arc.namelist(), KaggleLabelClassifier)
def main(args): batch_size = 6 if FLAGS.mode == 'decode': batch_size = FLAGS.beam_size hps = ABS.HParams( batch_size=batch_size, mode=FLAGS.mode, num_softmax_samples=4056, min_lr=0.01, lr=0.9, hid_dim=400, emb_dim=200, max_grad_norm=2, min_input_len=2, # discard articles/summaries < than this C=5, Q=2, dec_timesteps=90, enc_timesteps=520) vocab = Data.Vocab(FLAGS.vocab_path) batcher = BatchReader.Batcher( vocab, hps, max_article_sentences=FLAGS.max_article_sentences, max_abstract_sentences=FLAGS.max_abstract_sentences, ) vocab_size = vocab.NumIds() pad_id = vocab.WordToId(Data.PAD_TOKEN) tf.set_random_seed(FLAGS.random_seed) if hps.mode == 'train': model = ABS.ABS(hps, pad_id, vocab_size) _Train(model, batcher) elif hps.mode == 'decode': newhps = hps._replace(dec_timesteps=hps.C) model = ABS.ABS(hps, pad_id, vocab_size) decoder = Decode.BSDecoder(model, batcher, newhps, vocab) decoder.DecodeLoop()
def main(argv=None): # define placeholder{keep_probability,image,annotation} keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image") annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation") # execute inference then get pred_annotation, logits, and loss pred_annotation, logits = inference(image, keep_probability) tf.summary.image("input_image", image, max_outputs=2) tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2) tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2) loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3]), name="entropy"))) tf.summary.scalar("entropy", loss) trainable_var = tf.trainable_variables() train_op = train(loss, trainable_var) print("Setting up summary op...") summary_op = tf.summary.merge_all() print("Setting up image reader...") # Data_zoo/MIT_SceneParsing/ train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir) print(len(train_records)) print(len(valid_records)) print("Setting up dataset reader") image_options = {'resize': True, 'resize_size': IMAGE_SIZE} if FLAGS.mode == 'train': train_dataset_reader = dataset.BatchDatset(train_records, image_options) validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver() summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph) sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...") if FLAGS.mode == "train": for itr in range(MAX_ITERATION): train_images, train_annotations = train_dataset_reader.next_batch( FLAGS.batch_size) feed_dict = { image: train_images, annotation: train_annotations, keep_probability: 0.85 } sess.run(train_op, feed_dict=feed_dict) if itr % 10 == 0: train_loss, summary_str = sess.run([loss, summary_op], feed_dict=feed_dict) print("Step: %d, Train_loss:%g" % (itr, train_loss)) summary_writer.add_summary(summary_str, itr) if itr % 500 == 0: valid_images, valid_annotations = validation_dataset_reader.next_batch( FLAGS.batch_size) valid_loss = sess.run(loss, feed_dict={ image: valid_images, annotation: valid_annotations, keep_probability: 1.0 }) print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss)) saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr) elif FLAGS.mode == "visualize": valid_images, valid_annotations = validation_dataset_reader.get_random_batch( FLAGS.batch_size) pred = sess.run(pred_annotation, feed_dict={ image: valid_images, annotation: valid_annotations, keep_probability: 1.0 }) # single channle valid_annotations = np.squeeze(valid_annotations, axis=3) pred = np.squeeze(pred, axis=3) for itr in range(FLAGS.batch_size): utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5 + itr)) utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5 + itr)) utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5 + itr)) print("Saved image: %d" % itr)
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nkerns=[20, 50], batch_size=500): """ Demonstrates lenet on MNIST dataset :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: path to the dataset used for training /testing (MNIST here) :type nkerns: list of ints :param nkerns: number of kernels on each layer """ rng = numpy.random.RandomState(23455) br = BatchReader.inputs() br2 = BatchReader.inputs(testingData = True) X, Y = br.getNPArray(2) train_set_x = theano.shared(X[0:50000*0.6].astype(float)) train_set_y = theano.shared(Y[0:50000*0.6].astype('int32')) valid_set_x = theano.shared(X[50000*0.6:50000*0.9].astype(float)) valid_set_y = theano.shared(Y[50000*0.6:50000*0.9].astype('int32')) test_set_x = theano.shared(X[50000*0.9:50000].astype(float)) test_set_y = theano.shared(Y[50000*0.9:50000].astype('int32')) #datasets = load_data(dataset) #train_set_x, train_set_y = datasets[0] #valid_set_x, valid_set_y = datasets[1] #test_set_x, test_set_y = datasets[2] # compute number of minibatches for training, validation and testing n_train_batches = train_set_x.get_value(borrow=True).shape[0] n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_train_batches /= batch_size n_valid_batches /= batch_size n_test_batches /= batch_size # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch # start-snippet-1 x = T.matrix('x') # the data is presented as rasterized images y = T.ivector('y') # the labels are presented as 1D vector of # [int] labels ###################### # BUILD ACTUAL MODEL # ###################### print '... building the model' # Reshape matrix of rasterized images of shape (batch_size, 28 * 28) # to a 4D tensor, compatible with our LeNetConvPoolLayer # (28, 28) is the size of MNIST images. layer0_input = x.reshape((batch_size, 1, 48, 48)) # Construct the first convolutional pooling layer: # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24) # maxpooling reduces this further to (24/2, 24/2) = (12, 12) # 4D output tensor is thus of shape (batch_size, nkerns[0], 12, 12) layer0 = LeNetConvPoolLayer( rng, input=layer0_input, image_shape=(batch_size, 1, 48, 48), filter_shape=(nkerns[0], 1, 9, 9), poolsize=(2, 2) ) # Construct the second convolutional pooling layer # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8) # maxpooling reduces this further to (8/2, 8/2) = (4, 4) # 4D output tensor is thus of shape (batch_size, nkerns[1], 4, 4) layer1 = LeNetConvPoolLayer( rng, input=layer0.output, image_shape=(batch_size, nkerns[0], 20, 20), filter_shape=(nkerns[1], nkerns[0], 8, 8), poolsize=(2, 2) ) # the HiddenLayer being fully-connected, it operates on 2D matrices of # shape (batch_size, num_pixels) (i.e matrix of rasterized images). # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4), # or (500, 50 * 4 * 4) = (500, 800) with the default values. layer2_input = layer1.output.flatten(2) # construct a fully-connected sigmoidal layer layer2 = HiddenLayer( rng, input=layer2_input, n_in=nkerns[1] * 6 * 6, n_out=500, activation=T.tanh ) # classify the values of the fully-connected sigmoidal layer layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) # the cost we minimize during training is the NLL of the model cost = layer3.negative_log_likelihood(y) print type(test_set_x) # create a function to compute the mistakes that are made by the model test_model = theano.function( [index], layer3.errors(y), givens={ x: test_set_x[index * batch_size: (index + 1) * batch_size], y: test_set_y[index * batch_size: (index + 1) * batch_size] } ) validate_model = theano.function( [index], layer3.errors(y), givens={ x: valid_set_x[index * batch_size: (index + 1) * batch_size], y: valid_set_y[index * batch_size: (index + 1) * batch_size] } ) # create a list of all model parameters to be fit by gradient descent params = layer3.params + layer2.params + layer1.params + layer0.params # create a list of gradients for all model parameters grads = T.grad(cost, params) # train_model is a function that updates the model parameters by # SGD Since this model has many parameters, it would be tedious to # manually create an update rule for each model parameter. We thus # create the updates list by automatically looping over all # (params[i], grads[i]) pairs. updates = [ (param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(params, grads) ] train_model = theano.function( [index], cost, updates=updates, givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size] } ) # end-snippet-1 ############### # TRAIN MODEL # ############### print '... training' # early-stopping parameters patience = 10000 # look as this many examples regardless patience_increase = 2 # wait this much longer when a new best is # found improvement_threshold = 0.995 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience / 2) # go through this many # minibatche before checking the network # on the validation set; in this case we # check every epoch best_validation_loss = numpy.inf best_iter = 0 test_score = 0. start_time = timeit.default_timer() epoch = 0 done_looping = False while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): iter = (epoch - 1) * n_train_batches + minibatch_index if iter % 100 == 0: print 'training @ iter = ', iter cost_ij = train_model(minibatch_index) if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [validate_model(i) for i in xrange(n_valid_batches)] this_validation_loss = numpy.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % (epoch, minibatch_index + 1, n_train_batches, this_validation_loss * 100.)) # if we got the best validation score until now if this_validation_loss < best_validation_loss: #improve patience if loss improvement is good enough if this_validation_loss < best_validation_loss * \ improvement_threshold: patience = max(patience, iter * patience_increase) # save best validation score and iteration number best_validation_loss = this_validation_loss best_iter = iter # test it on the test set test_losses = [ test_model(i) for i in xrange(n_test_batches) ] test_score = numpy.mean(test_losses) print((' epoch %i, minibatch %i/%i, test error of ' 'best model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) if patience <= iter: done_looping = True break end_time = timeit.default_timer() print('Optimization complete.') print('Best validation score of %f %% obtained at iteration %i, ' 'with test performance %f %%' % (best_validation_loss * 100., best_iter + 1, test_score * 100.)) print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.)) print "Dumping parameters to ../data/convnet.pkl" dir = os.getcwd() path = os.path.join(dir,"../data") os.chdir(path) with open( "convnet.pkl" , 'wb') as file: dict = { "layer0" : layer0.param, "layer1" : layer1.param, "layer2" : layer2.param, "layer3" : layer3.param } pickle.dump(dict, file, 2 ) os.chdir(dir)
import BatchReader as dataset import tensorflow as tf import numpy as np max_it =int(1e4 + 1) IMAGE_HEIGHT = 64 IMAGE_WIDTH = 256 MAX_CAPTCHA = 5 CHAR_SET_LEN = 10 batch_size = 64 file = open('acc.txt','a') train_dataset_reader = dataset.BatchReader(index_file='index') X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT*IMAGE_WIDTH]) Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA*CHAR_SET_LEN]) keep_prob = tf.placeholder(tf.float32) # dropout # CNN def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1): x = tf.reshape(X, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) # 4 conv layer w_c1 = tf.Variable(w_alpha*tf.random_normal([3, 3, 1, 32])) b_c1 = tf.Variable(b_alpha*tf.random_normal([32])) conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1)) conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') conv1 = tf.nn.dropout(conv1, keep_prob) w_c2 = tf.Variable(w_alpha*tf.random_normal([3, 3, 32, 64])) b_c2 = tf.Variable(b_alpha*tf.random_normal([64])) conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2))
def main(argv=None): keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") learning_rate = tf.placeholder(tf.float32, name="learning_rate") image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 7], name="input_image") annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation") pred_annotation_value, pred_annotation, logits = inference(image, keep_probability) #logits:the last layer of conv net #labels:the ground truth at = float(cfgs.at) a_w = (1- 2*at) * tf.cast(tf.squeeze(annotation, squeeze_dims=[3]), tf.float32) + at pro = tf.nn.softmax(logits) loss_weight = tf.pow(1-tf.reduce_sum(pro * tf.one_hot(tf.squeeze(annotation, squeeze_dims=[3]), cfgs.NUM_OF_CLASSESS), 3), cfgs.gamma) loss = tf.reduce_mean(loss_weight * a_w * tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3]), name="entropy")) valid_loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3]), name="valid_entropy"))) tf.summary.scalar("train_loss", loss) tf.summary.scalar("valid_loss", valid_loss) tf.summary.scalar("learning_rate", learning_rate) trainable_var = tf.trainable_variables() if cfgs.debug: for var in trainable_var: utils.add_to_regularization_and_summary(var) train_op = train(loss, trainable_var, learning_rate) print("Setting up summary op...") summary_op = tf.summary.merge_all() #Check if has the log file if not os.path.exists(cfgs.logs_dir): print("The logs path '%s' is not found" % cfgs.logs_dir) print("Create now..") os.makedirs(cfgs.logs_dir) print("%s is created successfully!" % cfgs.logs_dir) #Create a file to write logs. #filename='logs'+ cfgs.mode + str(datatime.datatime.now()) + '.txt' filename="logs_%s%s.txt"%(cfgs.mode,datetime.datetime.now()) path_=os.path.join(cfgs.logs_dir,filename) with open(path_,'w') as logs_file: logs_file.write("The logs file is created at %s.\n" % datetime.datetime.now()) logs_file.write("The model is ---%s---.\n" % cfgs.logs_dir) logs_file.write("The mode is %s\n"% (cfgs.mode)) logs_file.write("The train data batch size is %d and the validation batch size is %d\n."%(cfgs.batch_size,cfgs.v_batch_size)) logs_file.write("The data size is %d and the MAX_ITERATION is %d.\n" % (IMAGE_SIZE, MAX_ITERATION)) logs_file.write("Setting up image reader...") print("Setting up image reader...") train_records, valid_records = scene_parsing.my_read_dataset(cfgs.seq_list_path, cfgs.anno_path) print('number of train_records',len(train_records)) print('number of valid_records',len(valid_records)) with open(path_, 'a') as logs_file: logs_file.write('number of train_records %d\n' % len(train_records)) logs_file.write('number of valid_records %d\n' % len(valid_records)) path_lr = cfgs.learning_rate_path with open(path_lr, 'r') as f: lr_ = float(f.readline().split('\n')[0]) print("Setting up dataset reader") vis = True if cfgs.mode == 'all_visualize' else False image_options = {'resize': True, 'resize_size': IMAGE_SIZE, 'visualize': vis, 'annotation':True} if cfgs.mode == 'train': train_dataset_reader = dataset.BatchDatset(train_records, image_options) validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) #save current train itr from stopping(init = 0) current_itr_var = tf.Variable(0, dtype=tf.int32, name='current_itr') sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver() summary_writer = tf.summary.FileWriter(cfgs.logs_dir, sess.graph) sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(cfgs.logs_dir) #if not train,restore the model trained before if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...") num_=0 current_itr = current_itr_var.eval(sess) print('itr\tmode\tlr_\tacc\tiou_acc\tloss') time_begin = time.time() valid_time = 100 for itr in xrange(current_itr, MAX_ITERATION): with open(path_lr, 'r') as f: lr_ = float(f.readline().split('\n')[0]) train_images, train_annotations, if_finish, epoch_no= train_dataset_reader.next_batch(cfgs.batch_size) if if_finish: print('Epochs%d finished, time comsumed:%.5f' % (epoch_no, time.time()-time_begin)) time_begin = time.time() feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85, learning_rate: lr_} sess.run(train_op, feed_dict=feed_dict) logs_file = open(path_, 'a') if itr % 10 == 0: train_loss, summary_str = sess.run([loss, summary_op], feed_dict=feed_dict) print('%d\t%s\t%g\t%s\t%s\t%.6f' % (itr, 'train',lr_, 'None','None',train_loss)) summary_writer.add_summary(summary_str, itr) if itr % valid_time == 0: with open('time_to_valid.txt', 'r') as f: valid_time = int(f.readline().split('\n')[0]) #Caculate the accurary at the training set. train_random_images, train_random_annotations = train_dataset_reader.get_random_batch_for_train(cfgs.v_batch_size) train_loss,train_pred_anno = sess.run([loss,pred_annotation], feed_dict={image:train_random_images, annotation:train_random_annotations, keep_probability:1.0}) accu_iou_,accu_pixel_ = accu.caculate_accurary(train_pred_anno, train_random_annotations) print('%d\t%s\t%g\t%.5f\t%.5f\t%.6f' % (itr, 'train', lr_, accu_pixel_, accu_iou_, train_loss)) #Output the logs. num_ = num_ + 1 logs_file.write('%d\t%s\t%g\t%.5f\t%.5f\t%.6f\n' % (itr, 'train', lr_, accu_pixel_, accu_iou_, train_loss)) valid_images, valid_annotations, _, _= validation_dataset_reader.next_batch(cfgs.v_batch_size) #valid_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations, #keep_probability: 1.0}) valid_loss_,pred_anno=sess.run([loss,pred_annotation],feed_dict={image:valid_images, annotation:valid_annotations, keep_probability:1.0}) accu_iou,accu_pixel=accu.caculate_accurary(pred_anno,valid_annotations) print('%d\t%s\t%g\t%.5f\t%.5f\t%.6f' % (itr, 'valid', lr_, accu_pixel, accu_iou, valid_loss_)) #Output the logs. logs_file.write('%d\t%s\t%g\t%.5f\t%.5f\t%.6f\n' % (itr, 'train', lr_, accu_pixel, accu_iou, valid_loss_)) #record current itr sess.run(tf.assign(current_itr_var, itr)) saver.save(sess, cfgs.logs_dir + "model.ckpt", itr) #End the iterator ''' if itr % 10000 == 0 and itr > 0: lr_ = lr_/10 print('learning rate change from %g to %g' %(lr_*10, lr_)) logs_file.write('learning rate change from %g to %g\n' %(lr_*10, lr_))''' logs_file.close()
def get_data_video(self): with tf.device('/cpu:0'): self.video_dataset_reader = dataset.BatchDatset(self.valid_records, self.image_options)
def get_data(self): with tf.device('/cpu:0'): if self.mode == 'train': self.train_dataset_reader = dataset.BatchDatset(self.train_records, self.image_options) self.validation_dataset_reader = dataset.BatchDatset(self.valid_records, self.image_options)
for angle in range(0,360,60): y_k.append(self.eval_net( self.rotate( x[k*self.batch_size:(k+1)*self.batch_size],angle) ) ) y_i.append(np.float32(y_k).mean(axis=0)) y = np.vstack(y_i) return y if __name__ == '__main__': rng = numpy.random.RandomState(42) br = BatchReader.inputs() br2 = BatchReader.inputs(testingData = True) X, Y = br.getNPArray(2) testX = br2.getNPArray(2) nkerns = [35,70,35] print X.shape, Y.shape print testX.shape n_examples = X.shape[0] fs = foldsize = n_examples/K_FOLD # k folds for k in range(K_FOLD): trainX = np.vstack(
cur_frame = line[cfgs.seq_num + 1].split(' ')[0] cur_frame_s = cur_frame.split("/") filename = '%s%s' % (cur_frame_s[len(cur_frame_s) - 3], os.path.splitext(cur_frame_s[-1])[0]) print(filename) #anno_frame = os.path.join(anno_folder, filename+'.bmp') record = { 'current': cur_frame, 'seq': seq_set, 'filename': filename } #print(record) seq_list[directory].append(record) random.shuffle(seq_list[directory]) no_of_images = len(seq_list[directory]) print('No of %s files %d' % (directory, no_of_images)) return seq_list if __name__ == '__main__': training_records, validation_records = my_read_dataset( cfgs.seq_list_path, cfgs.anno_path) image_options = {'resize': True, 'resize_size': 224} train_reader = dataset.BatchDatset(training_records, image_options) train_reader._read_images()
def main(argv=None): keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 7], name="input_image") annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation") pred_annotation_value, pred_annotation, logits, pred_prob = inference( image, keep_probability) tf.summary.image("input_image", image, max_outputs=2) tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2) tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2) #logits:the last layer of conv net #labels:the ground truth loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3]), name="entropy"))) tf.summary.scalar("entropy", loss) trainable_var = tf.trainable_variables() if cfgs.debug: for var in trainable_var: utils.add_to_regularization_and_summary(var) print("Setting up summary op...") summary_op = tf.summary.merge_all() #Create a file to write logs. #filename='logs'+ cfgs.mode + str(datetime.datetime.now()) + '.txt' filename = "logs_%s%s.txt" % (cfgs.mode, datetime.datetime.now()) path_ = os.path.join(cfgs.logs_dir, filename) logs_file = open(path_, 'w') logs_file.write("The logs file is created at %s\n" % datetime.datetime.now()) logs_file.write("The mode is %s\n" % (cfgs.mode)) logs_file.write( "The train data batch size is %d and the validation batch size is %d.\n" % (cfgs.batch_size, cfgs.v_batch_size)) logs_file.write("The train data is %s.\n" % (cfgs.data_dir)) logs_file.write("The model is ---%s---.\n" % cfgs.logs_dir) print("Setting up image reader...") logs_file.write("Setting up image reader...\n") train_records, valid_records = scene_parsing.my_read_video_dataset( cfgs.seq_list_path, cfgs.anno_path) print('number of train_records', len(train_records)) print('number of valid_records', len(valid_records)) logs_file.write('number of train_records %d\n' % len(train_records)) logs_file.write('number of valid_records %d\n' % len(valid_records)) print("Setting up dataset reader") vis = True if cfgs.mode == 'all_visualize' else False image_options = { 'resize': True, 'resize_size': IMAGE_SIZE, 'visualize': vis } if cfgs.mode == 'train': train_dataset_reader = dataset.BatchDatset(train_records, image_options) validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver() summary_writer = tf.summary.FileWriter(cfgs.logs_dir, sess.graph) sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(cfgs.logs_dir) #if not train,restore the model trained before if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...") if cfgs.mode == "accurary": count = 0 if_con = True accu_iou_t = 0 accu_pixel_t = 0 while if_con: count = count + 1 valid_images, valid_annotations, valid_filenames, if_con, start, end = validation_dataset_reader.next_batch_valid( cfgs.v_batch_size) valid_loss, pred_anno = sess.run( [loss, pred_annotation], feed_dict={ image: valid_images, annotation: valid_annotations, keep_probability: 1.0 }) accu_iou, accu_pixel = accu.caculate_accurary( pred_anno, valid_annotations) print("Ture %d ---> the data from %d to %d" % (count, start, end)) print("%s ---> Validation_pixel_accuary: %g" % (datetime.datetime.now(), accu_pixel)) print("%s ---> Validation_iou_accuary: %g" % (datetime.datetime.now(), accu_iou)) #Output logs. logs_file.write("Ture %d ---> the data from %d to %d\n" % (count, start, end)) logs_file.write("%s ---> Validation_pixel_accuary: %g\n" % (datetime.datetime.now(), accu_pixel)) logs_file.write("%s ---> Validation_iou_accuary: %g\n" % (datetime.datetime.now(), accu_iou)) accu_iou_t = accu_iou_t + accu_iou accu_pixel_t = accu_pixel_t + accu_pixel print("%s ---> Total validation_pixel_accuary: %g" % (datetime.datetime.now(), accu_pixel_t / count)) print("%s ---> Total validation_iou_accuary: %g" % (datetime.datetime.now(), accu_iou_t / count)) #Output logs logs_file.write("%s ---> Total validation_pixel_accurary: %g\n" % (datetime.datetime.now(), accu_pixel_t / count)) logs_file.write("%s ---> Total validation_iou_accurary: %g\n" % (datetime.datetime.now(), accu_iou_t / count)) elif cfgs.mode == "all_visualize": re_save_dir = "%s%s" % (cfgs.result_dir, datetime.datetime.now()) logs_file.write("The result is save at file'%s'.\n" % re_save_dir) logs_file.write("The number of part visualization is %d.\n" % cfgs.v_batch_size) #Check the result path if exists. if not os.path.exists(re_save_dir): print("The path '%s' is not found." % re_save_dir) print("Create now ...") os.makedirs(re_save_dir) print("Create '%s' successfully." % re_save_dir) logs_file.write("Create '%s' successfully.\n" % re_save_dir) re_save_dir_im = os.path.join(re_save_dir, 'images') re_save_dir_heat = os.path.join(re_save_dir, 'heatmap') re_save_dir_ellip = os.path.join(re_save_dir, 'ellip') re_save_dir_transheat = os.path.join(re_save_dir, 'transheat') if not os.path.exists(re_save_dir_im): os.makedirs(re_save_dir_im) if not os.path.exists(re_save_dir_heat): os.makedirs(re_save_dir_heat) if not os.path.exists(re_save_dir_ellip): os.makedirs(re_save_dir_ellip) if not os.path.exists(re_save_dir_transheat): os.makedirs(re_save_dir_transheat) count = 0 if_con = True accu_iou_t = 0 accu_pixel_t = 0 while if_con: count = count + 1 valid_images, valid_filename, valid_cur_images, if_con, start, end = validation_dataset_reader.next_batch_video_valid( cfgs.v_batch_size) pred_value, pred, logits_, pred_prob_ = sess.run( [pred_annotation_value, pred_annotation, logits, pred_prob], feed_dict={ image: valid_images, keep_probability: 1.0 }) print("Turn %d :----start from %d ------- to %d" % (count, start, end)) pred = np.squeeze(pred, axis=3) pred_value = np.squeeze(pred_value, axis=3) for itr in range(len(pred)): filename = valid_filename[itr]['filename'] valid_images_ = pred_visualize(valid_cur_images[itr].copy(), pred[itr]) utils.save_image(valid_images_.astype(np.uint8), re_save_dir_im, name="inp_" + filename) if cfgs.fit_ellip: #valid_images_ellip=fit_ellipse_findContours_ori(valid_images[itr].copy(),np.expand_dims(pred[itr],axis=2).astype(np.uint8)) valid_images_ellip = fit_ellipse_findContours( valid_cur_images[itr].copy(), np.expand_dims(pred[itr], axis=2).astype(np.uint8)) utils.save_image(valid_images_ellip.astype(np.uint8), re_save_dir_ellip, name="ellip_" + filename) if cfgs.heatmap: heat_map = density_heatmap(pred_prob_[itr, :, :, 1]) utils.save_image(heat_map.astype(np.uint8), re_save_dir_heat, name="heat_" + filename) if cfgs.trans_heat: trans_heat_map = translucent_heatmap( valid_cur_images[itr], heat_map.astype(np.uint8).copy()) utils.save_image(trans_heat_map, re_save_dir_transheat, name="trans_heat_" + filename)
import BatchReader as dataset import os import time from CaptchaModel import Captcha # load model model = Captcha() model.load_checkpoint('crack_capcha0.994400002956.model-9960') # load dataset dataset = dataset.BatchReader(ratio=0.01, test_size=1000) images, labels = dataset.get_val_batch(0, 1000) count = len(labels) correct = 0 # start timing start = time.time() t = start # start testing for i in range(count): image = images[i] label = labels[i] pred = model.predict(image) text = ''.join(map(str, pred)) if text == label: correct += 1 else: print(label, text) pass
for var in trainable_var: utils.add_to_regularization_and_summary(var) train_op = train(loss, trainable_var) print("Setting up summary op...") summary_op = tf.summary.merge_all() print("Setting up image reader...") train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir) print(len(train_records)) print(len(valid_records)) print("Setting up dataset reader") image_options = {'resize': True, 'resize_size': IMAGE_SIZE} if FLAGS.mode == 'train': train_dataset_reader = dataset.BatchDatset(train_records, image_options) validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver( max_to_keep=10) summary_writer = tf.summary.FileWriter(FLAGS.logs_dir+'/train', sess.graph) # tval_summary_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/val', sess.graph) sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...")
print(__doc__) from sklearn import neighbors, linear_model import BatchReader reader = BatchReader.inputs() array = reader.getNPArray(7809) X_digits = array[0] y_digits = array[1] n_samples = len(X_digits) X_train = X_digits[:.9*n_samples] y_train = y_digits[:.9*n_samples] X_test = X_digits[.9*n_samples:] y_test = y_digits[.9*n_samples:] logistic = linear_model.LogisticRegression() print('LogisticRegression score: %f' % logistic.fit(X_train, y_train).score(X_test,y_test))
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nkerns=[20, 50], batch_size=500): """ Demonstrates lenet on MNIST dataset :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: path to the dataset used for training /testing (MNIST here) :type nkerns: list of ints :param nkerns: number of kernels on each layer """ rng = numpy.random.RandomState(23455) br = BatchReader.inputs() br2 = BatchReader.inputs(testingData=True) X, Y = br.getNPArray(2) train_set_x = theano.shared(X[0:50000 * 0.6].astype(float)) train_set_y = theano.shared(Y[0:50000 * 0.6].astype('int32')) valid_set_x = theano.shared(X[50000 * 0.6:50000 * 0.9].astype(float)) valid_set_y = theano.shared(Y[50000 * 0.6:50000 * 0.9].astype('int32')) test_set_x = theano.shared(X[50000 * 0.9:50000].astype(float)) test_set_y = theano.shared(Y[50000 * 0.9:50000].astype('int32')) #datasets = load_data(dataset) #train_set_x, train_set_y = datasets[0] #valid_set_x, valid_set_y = datasets[1] #test_set_x, test_set_y = datasets[2] # compute number of minibatches for training, validation and testing n_train_batches = train_set_x.get_value(borrow=True).shape[0] n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_train_batches /= batch_size n_valid_batches /= batch_size n_test_batches /= batch_size # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch # start-snippet-1 x = T.matrix('x') # the data is presented as rasterized images y = T.ivector('y') # the labels are presented as 1D vector of # [int] labels ###################### # BUILD ACTUAL MODEL # ###################### print '... building the model' # Reshape matrix of rasterized images of shape (batch_size, 28 * 28) # to a 4D tensor, compatible with our LeNetConvPoolLayer # (28, 28) is the size of MNIST images. layer0_input = x.reshape((batch_size, 1, 48, 48)) # Construct the first convolutional pooling layer: # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24) # maxpooling reduces this further to (24/2, 24/2) = (12, 12) # 4D output tensor is thus of shape (batch_size, nkerns[0], 12, 12) layer0 = LeNetConvPoolLayer(rng, input=layer0_input, image_shape=(batch_size, 1, 48, 48), filter_shape=(nkerns[0], 1, 9, 9), poolsize=(2, 2)) # Construct the second convolutional pooling layer # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8) # maxpooling reduces this further to (8/2, 8/2) = (4, 4) # 4D output tensor is thus of shape (batch_size, nkerns[1], 4, 4) layer1 = LeNetConvPoolLayer(rng, input=layer0.output, image_shape=(batch_size, nkerns[0], 20, 20), filter_shape=(nkerns[1], nkerns[0], 8, 8), poolsize=(2, 2)) # the HiddenLayer being fully-connected, it operates on 2D matrices of # shape (batch_size, num_pixels) (i.e matrix of rasterized images). # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4), # or (500, 50 * 4 * 4) = (500, 800) with the default values. layer2_input = layer1.output.flatten(2) # construct a fully-connected sigmoidal layer layer2 = HiddenLayer(rng, input=layer2_input, n_in=nkerns[1] * 6 * 6, n_out=500, activation=T.tanh) # classify the values of the fully-connected sigmoidal layer layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) # the cost we minimize during training is the NLL of the model cost = layer3.negative_log_likelihood(y) print type(test_set_x) # create a function to compute the mistakes that are made by the model test_model = theano.function( [index], layer3.errors(y), givens={ x: test_set_x[index * batch_size:(index + 1) * batch_size], y: test_set_y[index * batch_size:(index + 1) * batch_size] }) validate_model = theano.function( [index], layer3.errors(y), givens={ x: valid_set_x[index * batch_size:(index + 1) * batch_size], y: valid_set_y[index * batch_size:(index + 1) * batch_size] }) # create a list of all model parameters to be fit by gradient descent params = layer3.params + layer2.params + layer1.params + layer0.params # create a list of gradients for all model parameters grads = T.grad(cost, params) # train_model is a function that updates the model parameters by # SGD Since this model has many parameters, it would be tedious to # manually create an update rule for each model parameter. We thus # create the updates list by automatically looping over all # (params[i], grads[i]) pairs. updates = [(param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(params, grads)] train_model = theano.function( [index], cost, updates=updates, givens={ x: train_set_x[index * batch_size:(index + 1) * batch_size], y: train_set_y[index * batch_size:(index + 1) * batch_size] }) # end-snippet-1 ############### # TRAIN MODEL # ############### print '... training' # early-stopping parameters patience = 10000 # look as this many examples regardless patience_increase = 2 # wait this much longer when a new best is # found improvement_threshold = 0.995 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience / 2) # go through this many # minibatche before checking the network # on the validation set; in this case we # check every epoch best_validation_loss = numpy.inf best_iter = 0 test_score = 0. start_time = timeit.default_timer() epoch = 0 done_looping = False while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): iter = (epoch - 1) * n_train_batches + minibatch_index if iter % 100 == 0: print 'training @ iter = ', iter cost_ij = train_model(minibatch_index) if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [ validate_model(i) for i in xrange(n_valid_batches) ] this_validation_loss = numpy.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % (epoch, minibatch_index + 1, n_train_batches, this_validation_loss * 100.)) # if we got the best validation score until now if this_validation_loss < best_validation_loss: #improve patience if loss improvement is good enough if this_validation_loss < best_validation_loss * \ improvement_threshold: patience = max(patience, iter * patience_increase) # save best validation score and iteration number best_validation_loss = this_validation_loss best_iter = iter # test it on the test set test_losses = [ test_model(i) for i in xrange(n_test_batches) ] test_score = numpy.mean(test_losses) print((' epoch %i, minibatch %i/%i, test error of ' 'best model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) if patience <= iter: done_looping = True break end_time = timeit.default_timer() print('Optimization complete.') print( 'Best validation score of %f %% obtained at iteration %i, ' 'with test performance %f %%' % (best_validation_loss * 100., best_iter + 1, test_score * 100.)) print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.)) print "Dumping parameters to ../data/convnet.pkl" dir = os.getcwd() path = os.path.join(dir, "../data") os.chdir(path) with open("convnet.pkl", 'wb') as file: dict = { "layer0": layer0.param, "layer1": layer1.param, "layer2": layer2.param, "layer3": layer3.param } pickle.dump(dict, file, 2) os.chdir(dir)
def get_reader(filename): arc = ZipTarReader.auto(filename) return BatchReader.BatchReader(arc, arc.namelist(), KaggleLabelClassifier)
y_k = [] for angle in range(0, 360, 60): y_k.append( self.eval_net( self.rotate( x[k * self.batch_size:(k + 1) * self.batch_size], angle))) y_i.append(np.float32(y_k).mean(axis=0)) y = np.vstack(y_i) return y if __name__ == '__main__': rng = numpy.random.RandomState(42) br = BatchReader.inputs() br2 = BatchReader.inputs(testingData=True) X, Y = br.getNPArray(2) testX = br2.getNPArray(2) nkerns = [35, 70, 35] print X.shape, Y.shape print testX.shape n_examples = X.shape[0] fs = foldsize = n_examples / K_FOLD # k folds for k in range(K_FOLD): trainX = np.vstack([X[:k * fs], X[(k + 1) * fs:]])