def cnnExecuter(mode, dto_data_set, outputer): ##################################################### # variables ##################################################### kstd.echoStart(mode) kstd.echoBlanks(2) x_size = dto_data_set.pixel_size x_1d = tf.placeholder(tf.float32, shape=[None, x_size]) y_size = dto_data_set.num_of_label_kind y_ans = tf.placeholder(tf.float32, shape=[None, y_size]) if mode == MODE_LEARNING: cnnLearning(dto_data_set, outputer, x_1d, y_ans) elif mode == MODE_RE_LEARNING: cnnReLearning(dto_data_set, outputer, x_1d, y_ans) if mode == MODE_VALIDATION: cnnValidation(dto_data_set, outputer, x_1d, y_ans) if mode == MODE_PREDICTION: cnnPrediction(dto_data_set, outputer, x_1d, y_ans) kstd.echoBlanks(2) kstd.echoIsAlready(mode)
def cnnPrediction(dto_data_set, outputer, x_1d, y_ans): process = "cnnPrediction" kstd.echoStart(process) kstd.echoBlanks(2) x_2d = tf.reshape(x_1d, [-1, dto_data_set.wigth, dto_data_set.height, 1]) ##################################################### # model learning ##################################################### y_cnn, y_acc = __cnnModel(x_2d, False) with tf.Session() as sess: saver = tf.train.Saver() saver.restore(sess, outputer.learned_parameter_file_path) x_target = dto_data_set.dtoNT_flat_img.getVariable() y_predicted = y_cnn.eval(feed_dict={x_1d: x_target}) dtoNT = kstd.DtoNpTable(dto_data_set.num_of_label_kind) dtoNT.addNpArray(y_predicted) dto_data_set.addValueTable(dtoNT) cnn.crateNLLabelFromValue(dto_data_set) outputer.PredictionResultSave(dto_data_set) sess.close()
def cnnValidation(dto_data_set, outputer, x_1d, y_ans): process = "cnnValidation" kstd.echoStart(process) kstd.echoBlanks(2) x_2d = tf.reshape(x_1d, [-1, dto_data_set.wigth, dto_data_set.height, 1]) ##################################################### # model learning ##################################################### y_cnn, y_acc = __cnnModel(x_2d, False) train_step, cross_entropy, accuracy = __cnnTrainingUnit( y_ans, y_cnn, y_acc) with tf.Session() as sess: #sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, outputer.learned_parameter_file_path) valid_x = dto_data_set.dtoNT_flat_img.getVariable() valid_y = dto_data_set.dtoNT_label.getVariable() train_accuracy = accuracy.eval(feed_dict={ x_1d: valid_x, y_ans: valid_y }) print("validation : %0.2f" % train_accuracy) sess.close() outputer.validationOutput(train_accuracy)
def cnnReLearning(dto_data_set, outputer, x_1d, y_ans): process = "cnnReLearning" kstd.echoStart(process) kstd.echoBlanks(2) x_2d = tf.reshape(x_1d, [-1, dto_data_set.wigth, dto_data_set.height, 1]) ##################################################### # model learning ##################################################### y_cnn, y_acc = __cnnModel(x_2d, True) train_step, cross_entropy, accuracy = __cnnTrainingUnit( y_ans, y_cnn, y_acc) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, outputer.learned_parameter_file_path) __cnnLearningUnit(dto_data_set, outputer, x_1d, y_ans, train_step, cross_entropy, accuracy) saver.save(sess, outputer.learned_parameter_file_path) sess.close()
def __cnnLearningUnit(dto_data_set, outputer, x_1d, y_ans, train_step, cross_entropy, accuracy): process = "learning unit" kstd.echoBlanks(5) kstd.echoStart(process) timer = kstd.timeCalculater() dto_data_set.fixDataSet() for ii in range(prop.LEARNING_ITERATION): batch_x, batch_y = dto_data_set.getBatchSet(prop.BATCH_SIZE) train_step.run(feed_dict={x_1d: batch_x, y_ans: batch_y}) if (ii + 1) % prop.CYCLE_LOG_OUTPUT == 0: train_accuracy = accuracy.eval(feed_dict={ x_1d: batch_x, y_ans: batch_y }) train_entropy = cross_entropy.eval(feed_dict={ x_1d: batch_x, y_ans: batch_y }) outputer.logOutput( getProgressMessage(ii, train_accuracy, train_entropy, timer)) timer.lap() #if (ii + 1 ) % prop.CYCLE_PARA_OUTPUT == 0: # saver.save(sess, outputer.learned_parameter_file_path) kstd.echoFinish(process)
if mode == MODE_VALIDATION: cnnValidation(dto_data_set, outputer, x_1d, y_ans) if mode == MODE_PREDICTION: cnnPrediction(dto_data_set, outputer, x_1d, y_ans) kstd.echoBlanks(2) kstd.echoIsAlready(mode) if __name__ == "__main__": argvs = sys.argv # コマンドライン引数を格納したリストの取得 argc = len(argvs) # 引数の個数 if not argc > 0: kstd.echoBlanks(2) kstd.echoBar() print("input 1:Learing 2:re-Learning 3:Validation 4:Prediction") elif argc > 1: outputer = cnn.OutputForTFCNN() case = 0 file_path = fi.filePath(case) path = file_path.learned_param outputer.setLearnedParameterFilePath(path) path = file_path.predicted_value outputer.setPredictedValueFilePath(path)