def train(args): queue_loader = Queue_loader(batch_size=args.b_size, num_epochs=args.ep) model = CNN(args.lr, args.b_size, queue_loader.num_batches) model.build(queue_loader.images) model.loss(queue_loader.labels) train_op = model.train() saver = tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run( tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) print('Start training') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: ep = 0 step = 1 while not coord.should_stop(): loss, _ = sess.run([model.loss_op, train_op]) if step % 10 == 0: print('epoch: %2d, step: %2d, loss: %.4f' % (ep + 1, step, loss)) if step % queue_loader.num_batches == 0: print('epoch: %2d, step: %2d, loss: %.4f, epoch %2d done.' % (ep + 1, step, loss, ep + 1)) checkpoint_path = os.path.join('data_log', 'cifar.ckpt') saver.save(sess, checkpoint_path, global_step=ep + 1) step = 1 ep += 1 else: step += 1 except tf.errors.OutOfRangeError: print('\nDone training, epoch limit: %d reached.' % (args.ep)) finally: coord.request_stop() coord.join(threads) sess.close() print('Done')
if os.path.exists(sys.argv[1]): image = load_single_image(sys.argv[1]) else: raise Exception("Image file " + sys.argv[1] + " does not exist") from cnn import CNN from misc.params import HyperParams import skimage.io import tensorflow as tf hyper = HyperParams(verbose=False) images_tf = tf.placeholder(tf.float32, [None, hyper.image_h, hyper.image_w, hyper.image_c], name="images") class_tf = tf.placeholder(tf.int64, [None], name='class') cnn = CNN() conv_last, gap, class_prob = cnn.build(images_tf) classmap = cnn.get_classmap(class_tf, conv_last) with tf.Session() as sess: tf.train.Saver().restore( sess, hyper.model_path ) conv_last_val, class_prob_val = sess.run([conv_last, class_prob], feed_dict={images_tf: image}) # use argsort instead of argmax to get all the classes class_predictions_all = class_prob_val.argsort(axis=1) roi_map = None for i in xrange(-1 * hyper.top_k,0): current_class = class_predictions_all[:,i] classmap_vals = sess.run(classmap, feed_dict={class_tf: current_class, conv_last: conv_last_val}) normalized_classmap = normalize(classmap_vals[0])
def main(): parser = argparse.ArgumentParser() parser.add_argument("-n", dest="new_split", action='store_true', help="Force a new data split to be created", default=False) parser.add_argument("-i", dest="iterations", type=int, nargs=1, help="Number of iterations to run", default=50) parser.add_argument("-f", dest="infile", type=str, help="Use a file as input", default="input.pkl") parser.add_argument("-e", dest="epochs", type=int, help="Specify a number of epochs to run", default=100) parser.add_argument( "-b", dest="batch_size", type=int, help="Specify the size of each batch to run through an epoch", default=100) argl = parser.parse_args() print(argl) if argl.new_split or not os.path.exists(INPUT_DIR + argl.infile): print("Making new data split: ", argl.infile) data_split = create_new_data_split(INPUT_DIR + argl.infile) else: print("Loading data split: ", argl.infile) data_split = load_data_split(INPUT_DIR + argl.infile) print(data_split.keys()) data = data_split['x_train'] print(data.shape) model = CNN.build() model.compile(optimizer=keras.optimizers.SGD(lr=0.1, momentum=0.9), loss='mean_squared_error') gray_data = list() for d in data: gray = cv2.cvtColor(d, cv2.COLOR_BGR2GRAY) gray = np.reshape(gray, (gray.shape[0], gray.shape[1], 1)) gray_data.append(gray) gray_data = np.array(gray_data) num_samples = data.shape[0] for i in range(argl.iterations): for e in range(argl.epochs): sample_idc = np.random.randint(0, num_samples, argl.batch_size) sampled_images = gray_data[sample_idc] real_images = data[sample_idc] model.train_on_batch(sampled_images, real_images) cv2.namedWindow("Compare") cv2.resizeWindow("Compare", 300, 400) while 1: sample = np.random.randint(0, num_samples, 1) gray = gray_data[sample] img = data[sample] predict = model.predict(gray) predict = np.reshape(predict, (32, 32, 3)) gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) all = np.hstack((img, gray, predict)) cv2.imshow("Compare", all) if cv2.waitKey(0) == ord('q'): break
acc = model.accuracy.eval(feed_dict={ model.x: test_images, model.y_: test_labels, model.keep_prob: 1.0 }) if i % 1000 == 0: print('\ntest accuracy %g' % acc) total_acc += acc bar.next() bar.finish() print('Avg. test accuracy %g' % (total_acc / FLAGS.test_steps)) # Add ops to save and restore all the variables. with tf.Session() as sess: model.build() sess.run(tf.global_variables_initializer()) print('Trying to restore model') try: model.saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_path)) print('Model restored') except: print('Could not restore model from checkpoint ' + FLAGS.checkpoint_path) if FLAGS.train: train_model(sess) if FLAGS.test: test_model(sess)
class MainProc(Proc): ''' @about 构造函数,只接受参数,不进行初始化 初始化在线程内完成 @param prop:程序配置模块 @return None ''' def __init__(self, prop, feed, msg_queue=None): sd = Shared() self.__logger = sd.getLogger() self.__prop = prop self.__feed = feed self.__msg_queue = msg_queue ''' @about 创建两个卷积网络对象 @param prop:程序配置对象 feed:数据提供对象 msg_queue:消息队列 @return None ''' def init(self): self.__isTrain = self.__prop.needTrain() self.__sess = tf.Session() self.__prop.updateAttr('session', self.__sess) self.__output_path = self.__prop.queryAttr('data_dir') self.__model_path = self.__prop.queryAttr('model_path') self.__ckpt_name = self.__prop.queryAttr('ckpt_name') self.__cnn_name = self.__prop.queryAttr('cnn_name') self.__batch_n = self.__prop.queryAttr('batch_n') self.__batch_h = self.__prop.queryAttr('batch_h') self.__batch_w = self.__prop.queryAttr('batch_w') self.__batch_c = self.__prop.queryAttr( 'sfeatures') + self.__prop.queryAttr('ifeatures') self.__cols = self.__prop.queryAttr('cols') # global photon cnn self.__global_fea = tf.placeholder( tf.float32, [self.__batch_n, self.__batch_h, self.__batch_w, self.__batch_c]) self.__global_img = tf.placeholder( tf.float32, [self.__batch_n, self.__batch_h, self.__batch_w, self.__cols]) self.__globalCNN = CNN(self.__prop, self.__cnn_name[0]) self.__globalCNN.build(self.__global_img, self.__global_fea) # self.__globalCNN.init() # caustic photon cnn self.__caustic_fea = tf.placeholder( tf.float32, [self.__batch_n, self.__batch_h, self.__batch_w, self.__batch_c]) self.__caustic_img = tf.placeholder( tf.float32, [self.__batch_n, self.__batch_h, self.__batch_w, self.__cols]) self.__causticCNN = CNN(self.__prop, self.__cnn_name[1]) self.__causticCNN.build(self.__caustic_img, self.__caustic_fea) # self.__causticCNN.init() # other configuration in train mode if self.__isTrain: self.__meta_name = self.__prop.queryAttr('meta_name') self.__loss_func = self.__prop.queryAttr('loss_func') self.__optimizer = self.__prop.queryAttr('optimizer') self.__max_round = self.__prop.queryAttr('max_round') self.__save_round = self.__prop.queryAttr('save_round') self.__learning_rate = self.__prop.queryAttr('learning_rate') ''' @about 线程调用入口 封装此模块主要流程 @param None @return None ''' def run(self): self.init() predict = self.preprocess(self.__prop) result = self.process(self.__feed, predict) self.postprocess(result) ''' @about 开启守护线程,执行此模块主要逻辑 @param None @return None ''' def start(self): wrk = Thread(target=self.run, args=(), name='Worker') wrk.setDaemon(True) wrk.start() ''' @about 向消息队列中发送数据 @param msg:[current status %,current loss] @return None ''' def sendMsg(self, msg): if platform.system() != 'Windows': return try: self.__msg_queue.put(msg) except: self.__logger.error('message queue is not specified.') ''' @about 优化器 @param loss:损失值 learning_rate:学习率 type:优化器类型 @return 优化器 ''' def __getOptimizer(self, loss, learning_rate, type): self.__logger.debug('building optimizer...') if type.lower() == 'adam': result = tf.train.AdamOptimizer(learning_rate).minimize(loss) elif type.lower() == 'gradient': result = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss) else: raise NotImplementedError self.__logger.debug('optimizer built.') return result ''' @about 损失函数,根据指定误差计算方式计算误差 @param input:预测值 output:目标值 type:损失函数类型 @return 损失值 ''' def __getLoss(self, input, output, type='l1'): self.__logger.debug('computing loss...') assert (input.shape == output.shape) if type == 'l1': result = tf.reduce_mean(tf.abs(tf.subtract(input, output))) elif type == 'cross_entropy': # 此方式表现不佳 result = tf.reduce_mean(-tf.reduce_sum(output * tf.log(input))) else: raise NotImplementedError self.__logger.debug('loss computed.') return result ''' @param @about @return ''' def __updateMeta(self, filename): print(filename) try: obj = utils.readJson(filename) obj['iterations'] += self.__save_round except: obj = {} obj['name'] = filename obj['iterations'] = self.__save_round obj['active_func'] = self.__prop.queryAttr('active_func') obj['weights_shape'] = self.__prop.queryAttr('weights_shape') utils.writeJson(obj, filename) ''' @about 两个卷积网络进行滤波,然后合并结果 @param prop: input2: @return 预测值,Tensor ''' def preprocess(self, prop): self.__logger.debug('preprocessing...') # filter images separately glob = self.__globalCNN.process(Filter(prop)) caus = self.__causticCNN.process(Filter(prop)) assert (glob.shape == caus.shape) predict = glob + caus return predict ''' @about 主要处理流程,处理来自preprocess的predict @param feed: 数据填充模块 predict:预测值 @return 处理过的predict ''' def process(self, feed, predict): self.__logger.debug('processing...') ishape, tshape = feed.getBatchShapes() # model storage path ckpt_global = self.__model_path + self.__cnn_name[0] + '/' ckpt_caustic = self.__model_path + self.__cnn_name[1] + '/' # train when in train mode if self.__isTrain: self.__logger.debug('training...') truth = tf.placeholder(tf.float32, ishape) # loss = tf.reduce_mean(tf.abs(tf.subtract(predict, truth))) loss = self.__getLoss(predict, truth, self.__loss_func) # step = tf.train.AdamOptimizer(self.__learning_rate).minimize(loss) step = self.__getOptimizer(loss, self.__learning_rate, self.__optimizer) # 恢复与随机初始化两网络 self.__globalCNN.init(ckpt_global) self.__causticCNN.init(ckpt_caustic) # 用于绘制进度条 # bar = LineProgress(title='status', total=self.__max_round) # 训练 for i in range(self.__max_round): gi, gf, ci, cf, gt = feed.next_batch() self.__sess.run(step, feed_dict={ self.__caustic_img: ci, self.__global_img: gi, self.__caustic_fea: cf, self.__global_fea: gf, truth: gt }) xloss = self.__sess.run(loss, feed_dict={ self.__caustic_img: ci, self.__global_img: gi, self.__caustic_fea: cf, self.__global_fea: gf, truth: gt }) self.sendMsg([i / self.__max_round, xloss]) # xpred = self.__sess.run(predict, feed_dict={self.__caustic_img: ci, self.__global_img: gi, # self.__caustic_fea: cf, self.__global_fea: gf, # truth: gt}) # utils.displayImage(xpred) print('round:%d of %d,loss: %f...' % (i + 1, self.__max_round, xloss)) self.__logger.info('round:%d of %d,loss:%f...' % (i + 1, self.__max_round, xloss)) # print("status: {:.2f}%".format(float((i + 1) / self.__max_round)), end="\r") # bar.update((i + 1) / self.__max_round * 100) # 保存结果 if i % self.__save_round == (self.__save_round - 1): self.__globalCNN.save(ckpt_global, self.__ckpt_name, self.__save_round) self.__causticCNN.save(ckpt_caustic, self.__ckpt_name, self.__save_round) for cnn in self.__cnn_name: path = self.__model_path + '/' + cnn + '/' + self.__meta_name self.__updateMeta(path) # infer模式下直接输出 else: self.__logger.debug('inferring...') # 恢复与随机初始化两网络 self.__globalCNN.init(ckpt_global) self.__causticCNN.init(ckpt_caustic) gi, gf, ci, cf = feed.getInputdata() result = self.__sess.run(predict, feed_dict={ self.__caustic_img: ci, self.__global_img: gi, self.__caustic_fea: cf, self.__global_fea: gf }) return result return None ''' @about process之后执行 @param input:process的输出 @return None ''' def postprocess(self, input1): sd = Shared() self.__logger.debug('postprocessing...') if not self.__isTrain: # path of test data file save_path = self.__output_path utils.saveImage(input1, save_path + 'infer.png') # utils.displayImage(input) sd.setFlag('nowExit', True) print('done') '''
data = np.array(data, dtype=np.float32) / 255.0 labels = np.array(labels) le = LabelEncoder().fit(labels) labels = np_utils.to_categorical(le.transform(labels), 2) classTotals = labels.sum(axis=0) classWeight = classTotals.max() / classTotals (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.20, stratify=labels) print("[INFO] compiling model...") model = CNN.build() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print("[INFO] training network...") checkpointer = ModelCheckpoint(filepath='lego.hdf5', verbose=1, save_best_only=True) H = model.fit(trainX, trainY, batch_size=32, epochs=40, validation_data=(testX, testY), callbacks=[checkpointer], verbose=1,
test_image_labels = np.array(test_image_labels) # turning labels vectors into matrices of shape (num_images, num_classes) train_labels_matrix = np_utils.to_categorical(train_image_labels, num_classes) test_labels_matrix = np_utils.to_categorical(test_image_labels, num_classes) # building the model # stochastic gradient descent sgd = optimizers.SGD(lr=0.1) # CNN takes images of size 64x64 dim = 64 # numChannels = 1 because the images are converted to grayscale model = CNN.build( numChannels=1, imgRows=dim, imgCols=dim, numClasses=num_classes, weightsPath=args["weights"] if args["load_model"] > 0 else None) # loss function in the paper was "mean_squared_error", but it didn't work well # so i used "categorical_crossentropy" model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"]) model.summary() # learning rate configurations from the paper def learning_rate_decay(epoch): init_learning_rate = 0.1 c1 = 50