def train(holdout=False, holdout_list=[]): # load data X, Y = load_X_and_Y() x_train, x_dev, x_test = X y_train, y_dev, y_test = Y # holdout training and dev data if requested if holdout: X_descr = load_X_descr() x_train_all_descr, x_dev_all_descr, _ = X_descr holdout_x_train = [] holdout_y_train = [] for idx in range(len(x_train)): if x_train_all_descr[idx] not in holdout_list: holdout_x_train.append(x_train[idx]) holdout_y_train.append(y_train[idx]) holdout_x_dev = [] holdout_y_dev = [] for idx in range(len(x_dev)): if x_dev_all_descr[idx] not in holdout_list: holdout_x_dev.append(x_dev[idx]) holdout_y_dev.append(y_dev[idx]) x_train = np.array(holdout_x_train).reshape((-1, 224, 224, 3)) y_train = np.array(holdout_y_train).reshape((-1, 1)) x_dev = np.array(holdout_x_dev).reshape((-1, 224, 224, 3)) y_dev = np.array(holdout_y_dev).reshape((-1, 1)) # train model model = CNN() model.fit(x_train, y_train, x_dev, y_dev, save=True) model.evaluate(x_test, y_test)
def main(): resize_shape = 64 print "data is loading..." train_X, train_Y, test_X, test_Y = load_data(resize_shape) print "data is loaded" print "feature engineering..." learning_rate = 0.01 training_iters = 100000 batch_size = 128 display_step = 10 # Network Parameters n_input = resize_shape*resize_shape # MNIST data input (img shape: 28*28) n_classes = 62 # MNIST total classes (0-9 digits) dropout = 0.5 # Dropout, probability to keep units with tf.Session() as sess: cnn = CNN(sess, learning_rate, training_iters, batch_size, display_step, n_input, n_classes, dropout,resize_shape) train_X = cnn.inference(train_X) test_X = cnn.inference(test_X) print "feature engineering is complete" print 'training phase' clf = svm.LinearSVC().fit(train_X, train_Y) print 'test phase' predicts = clf.predict(test_X) # measure function print 'measure phase' print confusion_matrix(test_Y, predicts) print f1_score(test_Y, predicts, average=None) print precision_score(test_Y, predicts, average=None) print recall_score(test_Y, predicts, average=None) print accuracy_score(test_Y, predicts)
def gridSearch(xTrain, yTrain, xDev, yDev, options): paramCombos = myProduct(options) bestCombo, bestCrossEntropy = None, float('inf') scores = {} for combo in paramCombos: cnn = CNN(numFilters=combo['numFilters'], windowSize=combo['windowSize']) cnn.fit(xTrain[:combo['numTrain']], yTrain[:combo['numTrain']], numEpochs=combo['numEpochs'], batchSize=combo['batchSize'], verbose=True) crossEntropy, accuracy = cnn.evaluate(xDev, yDev, showAccuracy=True) scores[tuple(combo.items())] = (crossEntropy, accuracy) if crossEntropy < bestCrossEntropy: bestCombo, bestCrossEntropy = combo, crossEntropy print 'Combo: {}, CE: {}, accuracy: {}'.format(combo, crossEntropy, accuracy) return scores
def __init__(self): self.run_recognition = rospy.get_param('/rgb_object_detection/run_recognition') self.model_file = rospy.get_param('/rgb_object_detection/model_file') self.category_file = rospy.get_param('/rgb_object_detection/category_file') self.verbose = rospy.get_param('/rgb_object_detection/verbose') self.bridge = CvBridge() self.image_sub = rospy.Subscriber('/camera/rgb/image_color', Image, self.img_cb) self.patches_sub = rospy.Subscriber('/candidate_regions_depth', PolygonStamped, self.patches_cb) self.detection_pub = rospy.Publisher('/detections', Detection) # you can read this value off of your sensor from the '/camera/depth_registered/camera_info' topic self.P = np.array([[525.0, 0.0, 319.5, 0.0], [0.0, 525.0, 239.5, 0.0], [0.0, 0.0, 1.0, 0.0]]) if self.run_recognition: self.cnn = CNN(self.model_file, self.category_file, self.verbose)
# -*- coding: utf-8 -*- from cnn import CNN import pandas as pd cnn = CNN() data=pd.DataFrame() data['cnn_feature']=0 data['cnn_feature']=data['cnn_feature'].astype(object) featurelist = cnn.get_features(['/home/seonhoon/Desktop/workspace/ImageQA/version_tensorflow/web/images/moodo.jpg'], layer='fc7') data.loc[0,'cnn_feature']=featurelist[0].flatten() data.to_pickle('/home/seonhoon/Desktop/workspace/ImageQA/version_tensorflow/web/cnn.pkl')
def main(): for k, v in a._get_kwargs(): print(k, "=", v) with open(os.path.join(a.output_dir, "options.json"), "w") as f: f.write(json.dumps(vars(a), sort_keys=True, indent=4)) loader = Loader(a.batch_size) # initialize models here model = CNN(a) if a.checkpoint is not None: print("loading model from checkpoint") model.load(a.checkpoint) if a.mode == 'test': if a.checkpoint is None: print('need checkpoint to continue') return draw(model, os.path.join(a.output_dir, 'test_output.jpg')) else: # training start = time.time() for epoch in range(a.max_epochs): def should(freq): return freq > 0 and ((epoch + 1) % freq == 0 or epoch == a.max_epochs - 1) training_loss = 0 for _ in range(loader.ntrain): X, y = loader.next_batch(0) model.step(X, y) training_loss += model.loss.data[0] training_loss /= loader.ntrain if should(a.validation_freq): print('validating model') validation_loss = 0 for _ in range(loader.nval): X, y = loader.next_batch(1) model.validate_step(X, y) validation_loss += model.loss.data[0] validation_loss /= loader.nval if should(a.summary_freq): print("recording summary") with open(os.path.join(a.output_dir, 'loss_record.txt'), "a") as loss_file: loss_file.write("%s\t%s\t%s\n" % (epoch, training_loss, validation_loss)) if should(a.progress_freq): rate = (epoch + 1) / (time.time() - start) remaining = (a.max_epochs - 1 - epoch) / rate print("progress epoch %d remaining %dh" % (epoch, remaining / 3600)) print("training loss", training_loss) if should(a.display_freq): draw(model, os.path.join(a.output_dir, '%s.jpg' % epoch)) if should(a.save_freq): print("saving model") model.save(os.path.join(a.output_dir, '%s.pth' % epoch))
accuracy = 100 * correct / total print('Test Accuracy of the model on the %d test images: %d %%' % (total, accuracy)) return accuracy # Hyper Parameters num_epochs = 100 batch_size = 100 learning_rate = 1e-5 wt_decay = 1e-7 lr_decay = 0.99 cnn = CNN() print("Fetching data...") train_dataset = get_augmented_dataset("./tiny-imagenet-200/train") test_dataset = get_dataset("./tiny-imagenet-200/val") train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size) # Loss and Optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate,
import paddle.fluid as fluid from PIL import Image import numpy as np from cnn import CNN # 使用动态图with块,指定使用GPU或者CPU预测 with fluid.dygraph.guard(place=fluid.CPUPlace()): # 获取网络结构 cnn_infer = CNN("mnist") # 加载模型参数 param_dict, _ = fluid.dygraph.load_dygraph("models/cnn") # 把参数加载到网络中 cnn_infer.load_dict(param_dict) # 开始执行预测 cnn_infer.eval() # 预处理数据 def load_image(file): im = Image.open(file).convert('L') im = im.resize((28, 28), Image.ANTIALIAS) im = np.array(im).reshape(1, 1, 28, 28).astype(np.float32) im = im / 255.0 * 2.0 - 1.0 return im # 获取预测数据 tensor_img = load_image('image/infer_3.png') # 执行预测 results = cnn_infer(fluid.dygraph.base.to_variable(tensor_img)) # 安装概率从大到小排序标签 lab = np.argsort(-results.numpy()) print("infer_3.png 预测的结果为: %d" % lab[0][0])
Y = binarizer.fit_transform(y) hidden_layer_sizes = [50] model = NeuralNet(hidden_layer_sizes) model.fit(X, Y) y_pred2 = model.predict(Xtest) v_error2 = np.mean(y_pred2 != ytest) print(v_error2) # CNN elif question == "1.5": with open(os.path.join('..', 'data', 'mnist.pkl'), 'rb') as f: train_set, valid_set, test_set = pickle.load(f, encoding="latin1") X, y = train_set Xtest, ytest = test_set model = CNN() model.fit(X, y) y_pred2 = model.predict(Xtest) v_error2 = np.mean(y_pred2 != ytest) print(v_error2) else: print("Unknown question: %s" % question)
class RGBObjectDetection: def __init__(self): self.run_recognition = rospy.get_param('/rgb_object_detection/run_recognition') self.model_file = rospy.get_param('/rgb_object_detection/model_file') self.category_file = rospy.get_param('/rgb_object_detection/category_file') self.verbose = rospy.get_param('/rgb_object_detection/verbose') self.bridge = CvBridge() self.image_sub = rospy.Subscriber('/camera/rgb/image_color', Image, self.img_cb) self.patches_sub = rospy.Subscriber('/candidate_regions_depth', PolygonStamped, self.patches_cb) self.detection_pub = rospy.Publisher('/detections', Detection) # you can read this value off of your sensor from the '/camera/depth_registered/camera_info' topic self.P = np.array([[525.0, 0.0, 319.5, 0.0], [0.0, 525.0, 239.5, 0.0], [0.0, 0.0, 1.0, 0.0]]) if self.run_recognition: self.cnn = CNN(self.model_file, self.category_file, self.verbose) def img_cb(self, msg): try: self.cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8") except CvBridgeError as e: print(e) def patches_cb(self, msg): if hasattr(self, 'cv_image'): ul_pc = msg.polygon.points[0] lr_pc = msg.polygon.points[1] cen_pc = msg.polygon.points[2] p1_pc = np.array([[ul_pc.x], [ul_pc.y], [ul_pc.z], [1.0]]) p2_pc = np.array([[lr_pc.x], [lr_pc.y], [lr_pc.z], [1.0]]) # transform from xyz (depth) space to xy (rgb) space, http://docs.ros.org/api/sensor_msgs/html/msg/CameraInfo.html p1_im = np.dot(self.P,p1_pc) p1_im = p1_im/p1_im[2] #scaling p2_im = np.dot(self.P,p2_pc) p2_im = p2_im/p2_im[2] #scaling p1_im_x = int(p1_im[0]) p1_im_y = int(p1_im[1]) p2_im_x = int(p2_im[0]) p2_im_y = int(p2_im[1]) # x is positive going right, y is positive going down width = p2_im_x - p1_im_x height = p1_im_y - p2_im_y # expand in y direction to account for angle of sensor expand_height = 0.4 # TODO: fix hack with transform/trig height_add = height * expand_height p1_im_y = p1_im_y + int(height_add) height = p1_im_y - p2_im_y # fix bounding box to be square diff = ( abs(width - height) / 2.0) if width > height: # update ys p1_im_y = int(p1_im_y + diff) p2_im_y = int(p2_im_y - diff) elif height > width: # update xs p1_im_x = int(p1_im_x - diff) p2_im_x = int(p2_im_x + diff) ## expand total box to create border around object (e.g. expand box 40%) expand_box = 0.4 box_add = (width * expand_box)/2.0 p1_im_x = int(p1_im_x - box_add) p1_im_y = int(p1_im_y + box_add) p2_im_x = int(p2_im_x + box_add) p2_im_y = int(p2_im_y - box_add) #plot expanded bounding box in green cv2.rectangle(self.cv_image, (p1_im_x, p1_im_y), (p2_im_x, p2_im_y), (0, 255, 0), thickness=5) # optional : run the recognition portion of the pipeline self.pred = '' self.pred_val = 0.0 if self.run_recognition: # crop image based on rectangle, note: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h] self.crop_img = self.cv_image[p2_im_y:p1_im_y, p1_im_x:p2_im_x] im = PImage.fromarray(self.crop_img, 'RGB') im = im.resize((self.cnn.sample_size, self.cnn.sample_size)) # resize im_gray = im.convert('L') # luma transform - L = R * 299/1000 + G * 587/1000 + B * 114/1000 im_gray_list = list(im_gray.getdata()) # make it into normal readable object im_gray_list_np = np.array(im_gray_list) / 255.0 im_pred = im_gray_list_np.reshape((1,1,self.cnn.sample_size,self.cnn.sample_size)) pred = self.cnn.predict(im_pred) self.pred = self.cnn.inv_catagories[np.argmax(pred,1)[0]] self.pred_val = np.max(pred,1)[0] font = cv2.FONT_HERSHEY_SIMPLEX label_text = str(self.pred) font_size = 1.0 font_thickness = 2 cv2.putText(self.cv_image, label_text, (p1_im_x,p1_im_y), font, font_size,(255,255,255), font_thickness) # publish detection message det_msg = Detection() det_msg.obj_class = self.pred det_msg.point = cen_pc self.detection_pub.publish(det_msg) # show image window cv2.imshow("Image window", self.cv_image) cv2.waitKey(3)
def keyword_search(): start = time() # web browser options = webdriver.ChromeOptions() options.add_argument('--ignore-certificate-errors') options.add_argument('--incognito') prefs = { "profile.managed_default_content_settings.images": 2, "profile.default_content_settings.images": 2, "disk-cache-size": 4096 } options.add_experimental_option("prefs", prefs) options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options) print(f'Browser creation took {(time() - start):.4f}s:') # setup args for site and keyword # get site to use, if none use all src = request.args.get('source') src = src.lower() if src is not None else src keyword = request.args.get('key') # make args for only some links sites = [] # select th sites to use if src is None: sites.append(CNN(driver)) sites.append(HuffPost(driver)) sites.append(WashPost(driver)) sites.append(Fox(driver)) sites.append(WSJ(driver)) sites.append(CBS(driver)) sites.append(WashTimes(driver)) sites.append(BiReport(driver)) sites.append(InfoWars(driver)) sites.append(NYT(driver)) sites.append(NBC(driver)) sites.append(CNS(driver)) elif src == "cnn": sites.append(CNN(driver)) elif src == "huffpost": sites.append(HuffPost(driver)) elif src == "washpost": sites.append(WashPost(driver)) elif src == "fox": sites.append(Fox(driver)) elif src == "wsj": sites.append(WSJ(driver)) elif src == "cbs": sites.append(CBS(driver)) elif src == "washtimes": sites.append(WashTimes(driver)) elif src == "bireport": sites.append(BiReport(driver)) elif src == "infowars": sites.append(InfoWars(driver)) elif src == "nyt": sites.append(NYT(driver)) elif src == "nbc": sites.append(NBC(driver)) elif src == "cns": sites.append(CNS(driver)) data = {'data': []} for site in sites: site_time = time() # get the links and its source links = site.get_links(keyword) link_src = site.__class__.__name__ for link in links: #edit title title = link[1] title = title.replace('\u2019', '\'') title = title.replace('\u2018', '\'') title = title.replace('\u201c', '"') title = title.replace('\u201d', '"') title = title.replace('\u2013', '-') title = title.replace('\u2014', '-') new_entry = { 'src': link_src, 'link': link[0], 'title': title, 'bias': site.bias_score } data['data'].append(new_entry) print(f'Reading "{link_src}" took {(time() - site_time):.4f}s') random.shuffle(data['data']) #json_str = json.dumps(data) response = jsonify(data) response.headers.add('Access-Control-Allow-Origin', '*') driver.quit() print(f'Total time for request took {(time() - start):.4f}s:') return response
def test_predict(): X = np.float32([[0.1, 0.2, 0.3, 0.4, 0.5, -0.1, -0.2, -0.3, -0.4, -0.5]]) X = np.float32([[0.1, 0.2, 0.3, 0.4, 0.5, 0, 0, 0, 0, 0]]) X = np.float32([np.linspace(0, 10, num=10)]) my_cnn = CNN() my_cnn.add_input_layer(shape=(10, ), name="input0") my_cnn.append_dense_layer(num_nodes=5, activation='linear', name="layer1") w = my_cnn.get_weights_without_biases(layer_name="layer1") w_set = np.full_like(w, 2) my_cnn.set_weights_without_biases(w_set, layer_name="layer1") b = my_cnn.get_biases(layer_name="layer1") b_set = np.full_like(b, 2) b_set[0] = b_set[0] * 2 my_cnn.set_biases(b_set, layer_name="layer1") actual = my_cnn.predict(X) assert np.array_equal(actual, np.array([[104., 102., 102., 102., 102.]]))
def test_remove_last_layer(): from tensorflow.keras.datasets import cifar10 batch_size = 32 num_classes = 10 epochs = 100 data_augmentation = True num_predictions = 20 save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'keras_cifar10_trained_model.h5' (X_train, y_train), (X_test, y_test) = cifar10.load_data() number_of_train_samples_to_use = 100 X_train = X_train[0:number_of_train_samples_to_use, :] y_train = y_train[0:number_of_train_samples_to_use] my_cnn = CNN() my_cnn.add_input_layer(shape=(32, 32, 3), name="input") my_cnn.append_conv2d_layer(num_of_filters=16, kernel_size=(3, 3), padding="same", activation='linear', name="conv1") my_cnn.append_maxpooling2d_layer(pool_size=2, padding="same", strides=2, name="pool1") my_cnn.append_conv2d_layer(num_of_filters=8, kernel_size=3, activation='relu', name="conv2") my_cnn.append_flatten_layer(name="flat1") my_cnn.append_dense_layer(num_nodes=10, activation="relu", name="dense1") my_cnn.append_dense_layer(num_nodes=2, activation="relu", name="dense2") out = my_cnn.predict(X_train) assert out.shape == (number_of_train_samples_to_use, 2) my_cnn.remove_last_layer() out = my_cnn.predict(X_train) assert out.shape == (number_of_train_samples_to_use, 10)
def test_set_weights_without_biases(): my_cnn = CNN() image_size = (np.random.randint(32, 100), np.random.randint(20, 100), np.random.randint(3, 10)) number_of_conv_layers = np.random.randint(2, 10) my_cnn.add_input_layer(shape=image_size, name="input") previous_depth = image_size[2] for k in range(number_of_conv_layers): number_of_filters = np.random.randint(3, 100) kernel_size = np.random.randint(3, 9) my_cnn.append_conv2d_layer(num_of_filters=number_of_filters, kernel_size=(kernel_size, kernel_size), padding="same", activation='linear') w = my_cnn.get_weights_without_biases(layer_number=k + 1) w_set = np.full_like(w, 0.2) my_cnn.set_weights_without_biases(w_set, layer_number=k + 1) w_get = my_cnn.get_weights_without_biases(layer_number=k + 1) assert w_get.shape == w_set.shape previous_depth = number_of_filters pool_size = np.random.randint(2, 5) my_cnn.append_maxpooling2d_layer(pool_size=pool_size, padding="same", strides=2, name="pool1") my_cnn.append_flatten_layer(name="flat1") my_cnn.append_dense_layer(num_nodes=10) number_of_dense_layers = np.random.randint(2, 10) previous_nodes = 10 for k in range(number_of_dense_layers): number_of_nodes = np.random.randint(3, 100) kernel_size = np.random.randint(3, 9) my_cnn.append_dense_layer(num_nodes=number_of_nodes) w = my_cnn.get_weights_without_biases(layer_number=k + number_of_conv_layers + 4) w_set = np.full_like(w, 0.8) my_cnn.set_weights_without_biases(w_set, layer_number=k + number_of_conv_layers + 4) w_get = my_cnn.get_weights_without_biases(layer_number=k + number_of_conv_layers + 4) assert w_get.shape == w_set.shape previous_nodes = number_of_nodes
def test_load_and_save_model(): # Note: This test may take a long time to load the data my_cnn = CNN() my_cnn.load_a_model(model_name="VGG19") w = my_cnn.get_weights_without_biases(layer_name="block5_conv4") assert w.shape == (3, 3, 512, 512) w = my_cnn.get_weights_without_biases(layer_number=-1) assert w.shape == (4096, 1000) my_cnn.append_dense_layer(num_nodes=10) path = os.getcwd() file_path = os.path.join(path, "my_model.h5") my_cnn.save_model(model_file_name=file_path) my_cnn.load_a_model(model_name="VGG16") w = my_cnn.get_weights_without_biases(layer_name="block4_conv1") assert w.shape == (3, 3, 256, 512) my_cnn.load_a_model(model_file_name=file_path) os.remove(file_path) w = my_cnn.get_weights_without_biases(layer_number=-1) assert w.shape == (1000, 10)
def train(x_train, y_train, vocab_processor, x_dev, y_dev): config = get_tfconfig() with tf.Graph().as_default(): with tf.Session(config=config) as sess: with sess.as_default(): cnn = CNN(x_train.shape[1], y_train.shape[1], len(vocab_processor.vocabulary_), EMBEDDING_DIM, list(map(int, FILTER_SIZES.split(","))), NUM_FILTERS, L2_REG_LAMBDA) gstep = tf.Variable(0, name="gstep", trainable=False) optimizer = tf.train.AdamOptimizer(learning_rate=0.001) grads_vars = optimizer.compute_gradients(cnn.loss) train_op = optimizer.apply_gradients(grads_vars, global_step=gstep) # output dictionary for models time_stamp = str(int(time.time())) out_dir = os.path.abspath( os.path.join(os.path.curdir, "runs", time_stamp)) print("writing to {}".format(out_dir)) # gradients summaries grad_summaries = [] accuracy_list = [] for g, v in grads_vars: if g is not None: grad_hist_summary = tf.summary.histogram( "{}/grad/hist".format(v.name), g) grad_summaries.append(grad_hist_summary) ## sparsity - 얘는 뭐하는 친구였을까요? sparsity_summary = tf.summary.scalar( "{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g)) grad_summaries.append(sparsity_summary) grad_summaries_merged = tf.summary.merge(grad_summaries) ## loss_summary = tf.summary.scalar("loss", cnn.loss) acc_summary = tf.summary.scalar("accuracy", cnn.accuracy) ## train_summary_op = tf.summary.merge( [loss_summary, acc_summary, grad_summaries_merged]) train_summary_dir = os.path.join(out_dir, "summaries", "train") train_summary_writer = tf.summary.FileWriter( train_summary_dir, sess.graph) dev_summary_op = tf.summary.merge([loss_summary, acc_summary]) dev_summary_dir = os.path.join(out_dir, "summaries", "dev") dev_summary_writer = tf.summary.FileWriter( dev_summary_dir, sess.graph) ## checkpoint_dir = os.path.abspath( os.path.join(out_dir, "checkpoints")) checkpoint_prefix = os.path.join(checkpoint_dir, "model") if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) saver = tf.train.Saver(tf.global_variables(), max_to_keep=NUM_CHECKPOINTS) vocab_processor.save(os.path.join(out_dir, "vocab")) sess.run(tf.global_variables_initializer()) def train_step(x_bat, y_bat): feed_dict = { cnn.x: x_bat, cnn.y: y_bat, cnn.keep_prob: KEEP_PROB } _, step, summaries, loss, acc = sess.run([ train_op, gstep, train_summary_op, cnn.loss, cnn.accuracy ], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}. loss {}, acc {}".format( time_str, step, loss, acc)) train_summary_writer.add_summary(summaries, step) def dev_step(x_bat, y_bat, writer=None): feed_dict = { cnn.x: x_bat, cnn.y: y_bat, cnn.keep_prob: 1.0 } step, summaries, loss, acc = sess.run( [gstep, dev_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}. loss {}, acc {}".format( time_str, step, loss, acc)) ## max_acc = max( accuracy_list) if len(accuracy_list) > 2 else -1000 if max_acc < acc: print("update {} -> {}, length {}".format( max_acc, acc, len(accuracy_list))) accuracy_list.append(acc) if writer: writer.add_summary(summaries, step) data = list(zip(x_train, y_train)) batches = batch_iter(data, BATCH_SIZE, NUM_EPOCHS) for batch in batches: x_bat, y_bat = zip(*batch) train_step(x_bat, y_bat) cur_step = tf.train.global_step(sess, gstep) if cur_step % EVAL_EVERY == 0: print("\nEvaluation : ") dev_step(x_dev, y_dev, writer=dev_summary_writer) print("") if cur_step % CHECKPOINT_EVERY == 0: path = saver.save(sess, checkpoint_prefix, global_step=cur_step) print("Saved model checkpoint to {}\n".format(path)) if len(accuracy_list) > 6: max_acc = max(accuracy_list) mm = 5 for idx, iacc in enumerate(accuracy_list[-5:]): if max_acc > iacc: mm -= 1 if mm == 0: break
def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--batch-size', type=int, default=64, metavar='N', help='input batch size for training (default: 64)') parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=10, metavar='N', help='number of epochs to train (default: 10)') parser.add_argument('--lr', type=float, default=0.01, metavar='LR', help='learning rate (default: 0.01)') parser.add_argument('--momentum', type=float, default=0.5, metavar='M', help='SGD momentum (default: 0.5)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument( '--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--optimizer', type=str, default='sgd', help='which optimizer to use in training. Valid options are' + \ '\'sgd\' or \'kfac\'.') parser.add_argument('--save-model', action='store_true', default=False, help='For Saving the current Model') parser.add_argument( '--save-stats', type=str, default=None, help='name of file to save training loss and test loss and accuracy.') parser.add_argument('--test-every', type=int, default=None, help='test the model roughly every n examples') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) device = torch.device("cuda" if use_cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} train_loader = torch.utils.data.DataLoader(datasets.MNIST( '.', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader(datasets.MNIST( '.', train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ])), batch_size=args.test_batch_size, shuffle=True, **kwargs) model = CNN().to(device) if args.optimizer == 'sgd': optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) elif args.optimizer == 'kfac': optimizer = KFAC(model, F.nll_loss) train_stats = {} for epoch in range(1, args.epochs + 1): train(args, model, device, train_loader, test_loader, optimizer, epoch, train_stats) test(args, model, device, test_loader) if (args.save_model): torch.save(model.state_dict(), "mnist_cnn.pt")
currValLoss = 1e6 totalIter = cfg.TRAIN_NB / cfg.BatchSize LogFile = codecs.open(cfg.LogFile, "a") phase_train = tf.Variable(True, name='phase_train') x = tf.placeholder(tf.float32, shape=[None, WND_HEIGHT, WND_WIDTH]) SeqLens = tf.placeholder(shape=[cfg.BatchSize], dtype=tf.int32) x_expanded = tf.expand_dims(x, 3) #Inputs = CNNLight(x_expanded, phase_train, 'CNN_1') Inputs = CNN(x_expanded, phase_train, 'CNN_1') logits = RNN(Inputs, SeqLens, 'RNN_1') # Target params indices = tf.placeholder(dtype=tf.int64, shape=[None, 2]) values = tf.placeholder(dtype=tf.int32, shape=[None]) shape = tf.placeholder(dtype=tf.int64, shape=[2]) # Make targets targets = tf.SparseTensor(indices, values, shape) # Compute Loss losses = tf.nn.ctc_loss(targets, logits, SeqLens) loss = tf.reduce_mean(losses)
train_set = numpy.transpose(data_set[0], (2, 0, 1)) # train_set = numpy.transpose(data_set, (2, 0, 1)) # cnn2_output = load_RGB('data/kosode/cnn2_after_train_norm', file_num * motif_num) # cnn1_W = loadW('data/kosode_motif2/train/cnn1_after_train') # cnn2_W = loadW('data/kosode_motif2/train/cnn2_after_train') makeFolder() # 時間計測 time1 = time.clock() print '~~~CNN1~~~' cnn1 = CNN(train_set, filter_shape, filter_shift_list[0], input_shape, node_shape[1], cnn_pre_train_lr, cnn_pre_train_epoch, isRGB) output_list = cnn1.output() cnn_saveColorImage(output_list, node_shape[1], 'cnn1_before_train') output_list_norm = local_contrast_normalization(output_list) cnn_saveColorImage(output_list_norm, node_shape[1], 'cnn1_before_training_norm') cnn1.pre_train() # cnn1.setW(cnn1_W) output_list = cnn1.output() cnn_saveColorImage(output_list, node_shape[1], 'cnn1_after_train') output_list_norm = local_contrast_normalization(output_list) cnn_saveColorImage(output_list_norm, node_shape[1], 'cnn1_after_train_norm') print '~~~CNN2~~~'
def test_add_input_layer(): model = CNN() out = model.add_input_layer(shape=(256, 256, 3), name="input0") assert True
def makeprediction(config_file, data_file=None, out_path=None, out_file=None, gpu=None): #Open configuration file for this network config = ConfigParser.ConfigParser() config.read(config_file) #Set the device on which to perform these computations if gpu: theano.sandbox.cuda.use(gpu) theano.config.nvcc.flags='-use=fast=math' theano.config.allow_gc=False else: device = config.get('General', 'device') theano.sandbox.cuda.use(device) if (device != 'cpu'): theano.config.nvcc.flags='-use=fast=math' theano.config.allow_gc=False #------------------------------------------------------------------------------ starttime=time.clock() print '\nInitializing Network' if os.path.exists(config.get('General', 'directory')+config.get('Network', 'weights_folder')): network = CNN(weights_folder = config.get('General', 'directory')+config.get('Network', 'weights_folder'), activation = config.get('Network', 'activation')) else: print 'Error: Weights folder does not exist. Could not initialize network' return; #------------------------------------------------------------------------------ print 'Opening Data Files' if data_file: test_data = LoadData(directory = '', data_file_name = data_file) else: test_data = LoadData(directory = config.get('Testing Data', 'folders').split(','), data_file_name = config.get('Testing Data', 'data_file')) #------------------------------------------------------------------------------ init_time = time.clock() - starttime print "Initialization = " + `init_time` + " seconds" starttime = time.clock() print 'Making Predictions' if out_path and out_file: network.predict(test_data.get_data(), results_folder = out_path, name = out_file) elif out_path: network.predict(test_data.get_data(), results_folder = out_path, name = config.get('Testing', 'prediction_file')) elif out_file: network.predict(test_data.get_data(), results_folder = config.get('General', 'directory')+config.get('Testing', 'prediction_folder'), name = out_file) else: network.predict(test_data.get_data(), results_folder = config.get('General', 'directory')+config.get('Testing', 'prediction_folder'), name = config.get('Testing', 'prediction_file')) pred_time = time.clock() - starttime #------------------------------------------------------------------------------ print "Prediction Time = " + `pred_time` + " seconds" test_data.close()
def test_get_weights_without_biases_3(): my_cnn = CNN() image_size = (np.random.randint(32, 100), np.random.randint(20, 100), np.random.randint(3, 10)) number_of_conv_layers = np.random.randint(2, 10) my_cnn.add_input_layer(shape=image_size, name="input") previous_depth = image_size[2] for k in range(number_of_conv_layers): number_of_filters = np.random.randint(3, 100) kernel_size = np.random.randint(3, 9) my_cnn.append_conv2d_layer(num_of_filters=number_of_filters, kernel_size=(kernel_size, kernel_size), padding="same", activation='linear') actual = my_cnn.get_weights_without_biases(layer_number=k + 1) assert actual.shape == (kernel_size, kernel_size, previous_depth, number_of_filters) previous_depth = number_of_filters actual = my_cnn.get_weights_without_biases(layer_number=0) assert actual is None pool_size = np.random.randint(2, 5) my_cnn.append_maxpooling2d_layer(pool_size=pool_size, padding="same", strides=2, name="pool1") actual = my_cnn.get_weights_without_biases(layer_name="pool1") assert actual is None my_cnn.append_flatten_layer(name="flat1") actual = my_cnn.get_weights_without_biases(layer_name="flat1") assert actual is None my_cnn.append_dense_layer(num_nodes=10) number_of_dense_layers = np.random.randint(2, 10) previous_nodes = 10 for k in range(number_of_dense_layers): number_of_nodes = np.random.randint(3, 100) kernel_size = np.random.randint(3, 9) my_cnn.append_dense_layer(num_nodes=number_of_nodes) actual = my_cnn.get_weights_without_biases(layer_number=k + number_of_conv_layers + 4) previous_nodes = number_of_nodes
def main(): # VARIABLES - FILES # NOTE: Make sure to change these for different libraries! phone_file = conf.get_string("phone_file") # path to file of phone list dictionary_file = conf.get_string( "dictionary_file") # path to file of word list transcript_file = conf.get_string( "transcript_file") # path/directory of transcript file(s) transcript_type = conf.get_string( "transcript_type" ) # library being used (to find & parse transcript file) words_to_phones_file = conf.get_string( "words_to_phones_file") # path to file matching words with phones directory_path = conf.get_string( "directory_path") # directory containing audio files # VARIABLES - NUMBERS learning_rate = .2 epoch = 1 # Layer 1: filter_width = 2 # width of convolving filter filter_height = 13 # height of convolving filter filter_count = 3 # number of filters applied filter_stride = 1 # how far the filter moves between convolves filter_padding = 0 # padding around the input to use edge data (may not be useful with audio) downsample_count = 1 # how many times downsampling is performed downsample_multiplier = 2 # Layer 2: memory_dimension = 5 # number of hidden nodes in RNN # VARIABLES - FUNCTIONS activate = sigmoid d_activate = d_sigmoid # SETUP print("[INFO] Loading files") phones = load_list(phone_file) dictionary = ['<sil>'] + load_list(dictionary_file) transcript = load_transcript(transcript_file, transcript_type) words_to_phones = load_dictionary(dictionary_file) words_to_phones['<sil>'] = 'SIL' print("[INFO] Setting up features") audio_features, phone_features, word_features = setup_features( directory_path, phones, dictionary, transcript, words_to_phones) # GROUPING VARIABLES features = (audio_features, phone_features, word_features) # INITIALIZING # LAYER 1: AUDIO -> PHONES print("[INFO] Initializing LAYER 1: AUDIO -> PHONES") l1_input = np.matrix(audio_features[0]).shape l1_output = np.matrix(phone_features[0]).shape l1 = CNN( l1_input[0], l1_input[1], 1, # TODO: Change the inputs depending on the custom audio output filter_width, filter_height, filter_count, filter_stride, filter_padding, l1_output[0], l1_output[1], downsample_count, downsample_multiplier, activate, d_activate) # LAYER 2: PHONES -> WORDS print("[INFO] Initializing LAYER 2: PHONES -> WORDS") l2_input = l1_output l2_output = np.matrix(word_features[0]).shape l2 = RNN(l2_input[0], l2_input[1], l2_output[0], l2_output[1], memory_dimension, activate, d_activate) # NOTE: You can also simulate a sliding window using a CNN with a filter # height the same as the input height! # You might want to change the RNN to that if it produces better # results. # MENU command = '' trained = False while (command != '0'): print("===================================") print(' NEURAL NETWORK OPTIONS:') print(' [1] Train network') print(' [2] Load an existing network') if trained: print(' [3] Test network') print(' [4] Demo network') print(' [5] Save network') print(' [0] Quit') print("===================================") try: command = int(input('What would you like to do? ')) except (NameError, ValueError, SyntaxError): print('Invalid command!') continue else: if command == 0: # [0] Quit print('Bye!') break elif command == 1: # [1] Train network train_network(l1, l2, features, epoch, learning_rate, dictionary, phones) trained = True elif command == 2: # [2] Load an existing network print('[INFO] Attempting to load existing network') if load(l1, l2): trained = True elif command == 3 and trained: # [3] Test network # TODO: Testing function print('[ERROR] Not implemented') pass elif command == 4 and trained: # [4] Demo network # TODO: Demo function print('[ERROR] Not implemented') pass elif command == 5 and trained: # [5] Save network print('[INFO] Saving network') save(l1, l2) else: print('Invalid command!') return
import matplotlib.pyplot as plt CLASS_LABELS = ['apple','banana','nectarine','plum','peach','watermelon','pear','mango','grape','orange','strawberry','pineapple', 'radish','carrot','potato','tomato','bellpepper','broccoli','cabbage','cauliflower','celery','eggplant','garlic','spinach','ginger'] LITTLE_CLASS_LABELS = ['apple','banana','eggplant'] image_size = 90 random.seed(0) classes = CLASS_LABELS dm = data_manager(classes, image_size) cnn = CNN(classes,image_size) solver = Solver(cnn,dm) solver.optimize() plt.plot(solver.test_accuracy,label = 'Validation') plt.plot(solver.train_accuracy, label = 'Training') plt.legend() plt.xlabel('Iterations (in 200s)') plt.ylabel('Accuracy') plt.show() val_data = dm.val_data train_data = dm.train_data
def main(argv): print("CUDA_VISIBLE_DEVICES=", os.environ['CUDA_VISIBLE_DEVICES']) train_dir = FLAGS.train_dir dev_dir = FLAGS.dev_dir maps_dir = FLAGS.maps_dir if train_dir == '': print( 'Must supply input data directory generated from tsv_to_tfrecords.py' ) sys.exit(1) print('\n'.join( sorted([ "%s : %s" % (str(k), str(v)) for k, v in FLAGS.__dict__['__flags'].items() ]))) with open(maps_dir + '/label.txt', 'r') as f: labels_str_id_map = { l.split('\t')[0]: int(l.split('\t')[1].strip()) for l in f.readlines() } labels_id_str_map = {i: s for s, i in labels_str_id_map.items()} labels_size = len(labels_id_str_map) with open(maps_dir + '/token.txt', 'r') as f: vocab_str_id_map = { l.split('\t')[0]: int(l.split('\t')[1].strip()) for l in f.readlines() } vocab_id_str_map = {i: s for s, i in vocab_str_id_map.items()} vocab_size = len(vocab_id_str_map) with open(maps_dir + '/shape.txt', 'r') as f: shape_str_id_map = { l.split('\t')[0]: int(l.split('\t')[1].strip()) for l in f.readlines() } shape_id_str_map = {i: s for s, i in shape_str_id_map.items()} shape_domain_size = len(shape_id_str_map) with open(maps_dir + '/char.txt', 'r') as f: char_str_id_map = { l.split('\t')[0]: int(l.split('\t')[1].strip()) for l in f.readlines() } char_id_str_map = {i: s for s, i in char_str_id_map.items()} char_domain_size = len(char_id_str_map) # with open(maps_dir + '/sizes.txt', 'r') as f: # num_train_examples = int(f.readline()[:-1]) print("num classes: %d" % labels_size) size_files = [ maps_dir + "/" + fname for fname in listdir(maps_dir) if fname.find("sizes") != -1 ] num_train_examples = 0 num_tokens = 0 for size_file in size_files: print(size_file) with open(size_file, 'r') as f: num_train_examples += int(f.readline()[:-1]) num_tokens += int(f.readline()[:-1]) print("num train examples: %d" % num_train_examples) print("num train tokens: %d" % num_tokens) dev_top_dir = '/'.join( dev_dir.split("/")[:-2]) if dev_dir.find("*") != -1 else dev_dir print(dev_top_dir) dev_size_files = [ dev_top_dir + "/" + fname for fname in listdir(dev_top_dir) if fname.find("sizes") != -1 ] num_dev_examples = 0 num_dev_tokens = 0 for size_file in dev_size_files: print(size_file) with open(size_file, 'r') as f: num_dev_examples += int(f.readline()[:-1]) num_dev_tokens += int(f.readline()[:-1]) print("num dev examples: %d" % num_dev_examples) print("num dev tokens: %d" % num_dev_tokens) # with open(dev_dir + '/sizes.txt', 'r') as f: # num_dev_examples = int(f.readline()[:-1]) type_set = {} type_int_int_map = {} outside_set = ["O", "<PAD>", "<S>", "</S>", "<ZERO>"] for label, id in labels_str_id_map.items(): label_type = label if label in outside_set else label[2:] if label_type not in type_set: type_set[label_type] = len(type_set) type_int_int_map[id] = type_set[label_type] print(type_set) # load embeddings, if given; initialize in range [-.01, .01] embeddings_shape = (vocab_size - 1, FLAGS.embed_dim) embeddings = tf_utils.embedding_values(embeddings_shape, old=False) embeddings_used = 0 if FLAGS.embeddings != '': with open(FLAGS.embeddings, 'r') as f: for line in f.readlines(): split_line = line.strip().split(" ") word = split_line[0] embedding = split_line[1:] if word in vocab_str_id_map: embeddings_used += 1 # shift by -1 because we are going to add a 0 constant vector for the padding later embeddings[vocab_str_id_map[word] - 1] = map( float, embedding) elif word.lower() in vocab_str_id_map: embeddings_used += 1 embeddings[vocab_str_id_map[word.lower()] - 1] = map( float, embedding) print("Loaded %d/%d embeddings (%2.2f%% coverage)" % (embeddings_used, vocab_size, embeddings_used / vocab_size * 100)) layers_map = sorted(json.loads(FLAGS.layers.replace( "'", '"')).items()) if FLAGS.model == 'cnn' else None pad_width = int(layers_map[0][1]['width'] / 2) if layers_map is not None else 1 with tf.Graph().as_default(): train_batcher = Batcher( train_dir, FLAGS.batch_size) if FLAGS.memmap_train else SeqBatcher( train_dir, FLAGS.batch_size) dev_batch_size = FLAGS.batch_size # num_dev_examples dev_batcher = SeqBatcher(dev_dir, dev_batch_size, num_buckets=0, num_epochs=1) if FLAGS.ontonotes: domain_dev_batchers = { domain: SeqBatcher(dev_dir.replace('*', domain), dev_batch_size, num_buckets=0, num_epochs=1) for domain in ['bc', 'nw', 'bn', 'wb', 'mz', 'tc'] } train_eval_batch_size = FLAGS.batch_size train_eval_batcher = SeqBatcher(train_dir, train_eval_batch_size, num_buckets=0, num_epochs=1) char_embedding_model = BiLSTMChar(char_domain_size, FLAGS.char_dim, int(FLAGS.char_tok_dim/2)) \ if FLAGS.char_dim > 0 and FLAGS.char_model == "lstm" else \ (CNNChar(char_domain_size, FLAGS.char_dim, FLAGS.char_tok_dim, layers_map[0][1]['width']) if FLAGS.char_dim > 0 and FLAGS.char_model == "cnn" else None) char_embeddings = char_embedding_model.outputs if char_embedding_model is not None else None if FLAGS.model == 'cnn': model = CNN(num_classes=labels_size, vocab_size=vocab_size, shape_domain_size=shape_domain_size, char_domain_size=char_domain_size, char_size=FLAGS.char_tok_dim, embedding_size=FLAGS.embed_dim, shape_size=FLAGS.shape_dim, nonlinearity=FLAGS.nonlinearity, layers_map=layers_map, viterbi=FLAGS.viterbi, projection=FLAGS.projection, loss=FLAGS.loss, margin=FLAGS.margin, repeats=FLAGS.block_repeats, share_repeats=FLAGS.share_repeats, char_embeddings=char_embeddings, embeddings=embeddings) elif FLAGS.model == "bilstm": model = BiLSTM(num_classes=labels_size, vocab_size=vocab_size, shape_domain_size=shape_domain_size, char_domain_size=char_domain_size, char_size=FLAGS.char_dim, embedding_size=FLAGS.embed_dim, shape_size=FLAGS.shape_dim, nonlinearity=FLAGS.nonlinearity, viterbi=FLAGS.viterbi, hidden_dim=FLAGS.lstm_dim, char_embeddings=char_embeddings, embeddings=embeddings) else: print(FLAGS.model + ' is not a valid model type') sys.exit(1) # Define Training procedure global_step = tf.Variable(0, name='global_step', trainable=False) optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.lr, beta1=FLAGS.beta1, beta2=FLAGS.beta2, epsilon=FLAGS.epsilon, name="optimizer") model_vars = tf.global_variables() print("model vars: %d" % len(model_vars)) print(map(lambda v: v.name, model_vars)) # todo put in func total_parameters = 0 for variable in tf.trainable_variables(): # shape is an array of tf.Dimension shape = variable.get_shape() variable_parametes = 1 for dim in shape: variable_parametes *= dim.value total_parameters += variable_parametes print("Total trainable parameters: %d" % (total_parameters)) if FLAGS.clip_norm > 0: grads, _ = tf.clip_by_global_norm( tf.gradients(model.loss, model_vars), FLAGS.clip_norm) train_op = optimizer.apply_gradients(zip(grads, model_vars), global_step=global_step) else: train_op = optimizer.minimize(model.loss, global_step=global_step, var_list=model_vars) tf.global_variables_initializer() opt_vars = [ optimizer.get_slot(s, n) for n in optimizer.get_slot_names() for s in model_vars if optimizer.get_slot(s, n) is not None ] model_vars += opt_vars if FLAGS.load_dir: reader = tf.train.NewCheckpointReader(FLAGS.load_dir + ".tf") saved_var_map = reader.get_variable_to_shape_map() intersect_vars = [ k for k in tf.global_variables() if k.name.split(':')[0] in saved_var_map and k.get_shape() == saved_var_map[k.name.split(':')[0]] ] leftovers = [ k for k in tf.global_variables() if k.name.split(':')[0] not in saved_var_map or k.get_shape() != saved_var_map[k.name.split(':')[0]] ] print("WARNING: Loading pretrained model, but not loading: ", map(lambda v: v.name, leftovers)) loader = tf.train.Saver(var_list=intersect_vars) else: loader = tf.train.Saver(var_list=model_vars) saver = tf.train.Saver(var_list=model_vars) sv = tf.train.Supervisor( logdir=FLAGS.model_dir if FLAGS.model_dir != '' else None, global_step=global_step, saver=None, save_model_secs=0, save_summaries_secs=0) training_start_time = time.time() with sv.managed_session( FLAGS.master, config=tf.ConfigProto(allow_soft_placement=True)) as sess: def run_evaluation(eval_batches, extra_text=""): predictions = [] for b, (eval_label_batch, eval_token_batch, eval_shape_batch, eval_char_batch, eval_seq_len_batch, eval_tok_len_batch, eval_mask_batch) in enumerate(eval_batches): batch_size, batch_seq_len = eval_token_batch.shape char_lens = np.sum(eval_tok_len_batch, axis=1) max_char_len = np.max(eval_tok_len_batch) eval_padded_char_batch = np.zeros( (batch_size, max_char_len * batch_seq_len)) for b in range(batch_size): char_indices = [ item for sublist in [ range(i * max_char_len, i * max_char_len + d) for i, d in enumerate(eval_tok_len_batch[b]) ] for item in sublist ] eval_padded_char_batch[ b, char_indices] = eval_char_batch[b][:char_lens[b]] char_embedding_feeds = {} if FLAGS.char_dim == 0 else { char_embedding_model.input_chars: eval_padded_char_batch, char_embedding_model.batch_size: batch_size, char_embedding_model.max_seq_len: batch_seq_len, char_embedding_model.token_lengths: eval_tok_len_batch, char_embedding_model.max_tok_len: max_char_len } basic_feeds = { model.input_x1: eval_token_batch, model.input_x2: eval_shape_batch, model.input_y: eval_label_batch, model.input_mask: eval_mask_batch, model.max_seq_len: batch_seq_len, model.batch_size: batch_size, model.sequence_lengths: eval_seq_len_batch } basic_feeds.update(char_embedding_feeds) total_feeds = basic_feeds.copy() if FLAGS.viterbi: preds, transition_params = sess.run( [model.predictions, model.transition_params], feed_dict=total_feeds) viterbi_repad = np.empty((batch_size, batch_seq_len)) for batch_idx, (unary_scores, sequence_lens) in enumerate( zip(preds, eval_seq_len_batch)): viterbi_sequence, _ = tf.contrib.crf.viterbi_decode( unary_scores, transition_params) viterbi_repad[batch_idx] = viterbi_sequence predictions.append(viterbi_repad) else: preds, scores = sess.run( [model.predictions, model.unflat_scores], feed_dict=total_feeds) predictions.append(preds) if FLAGS.print_preds != '': evaluation.print_conlleval_format( FLAGS.print_preds, eval_batches, predictions, labels_id_str_map, vocab_id_str_map, pad_width) # print evaluation f1_micro, precision = evaluation.segment_eval( eval_batches, predictions, type_set, type_int_int_map, labels_id_str_map, vocab_id_str_map, outside_idx=map( lambda t: type_set[t] if t in type_set else type_set["O"], outside_set), pad_width=pad_width, start_end=FLAGS.start_end, extra_text="Segment evaluation %s:" % extra_text) return f1_micro, precision threads = tf.train.start_queue_runners(sess=sess) log_every = int(max(100, num_train_examples / 5)) if FLAGS.load_dir != '': print("Deserializing model: " + FLAGS.load_dir + ".tf") loader.restore(sess, FLAGS.load_dir + ".tf") def get_dev_batches(seq_batcher): batches = [] # load all the dev batches into memory done = False while not done: try: dev_batch = sess.run(seq_batcher.next_batch_op) dev_label_batch, dev_token_batch, dev_shape_batch, dev_char_batch, dev_seq_len_batch, dev_tok_len_batch = dev_batch mask_batch = np.zeros(dev_token_batch.shape) actual_seq_lens = np.add( np.sum(dev_seq_len_batch, axis=1), (2 if FLAGS.start_end else 1) * pad_width * ((dev_seq_len_batch != 0).sum(axis=1) + (0 if FLAGS.start_end else 1))) for i, seq_len in enumerate(actual_seq_lens): mask_batch[i, :seq_len] = 1 batches.append( (dev_label_batch, dev_token_batch, dev_shape_batch, dev_char_batch, dev_seq_len_batch, dev_tok_len_batch, mask_batch)) except: done = True return batches dev_batches = get_dev_batches(dev_batcher) if FLAGS.ontonotes: domain_batches = { domain: get_dev_batches(domain_batcher) for domain, domain_batcher in domain_dev_batchers.iteritems() } train_batches = [] if FLAGS.train_eval: # load all the train batches into memory done = False while not done: try: train_batch = sess.run( train_eval_batcher.next_batch_op) train_label_batch, train_token_batch, train_shape_batch, train_char_batch, train_seq_len_batch, train_tok_len_batch = train_batch mask_batch = np.zeros(train_token_batch.shape) actual_seq_lens = np.add( np.sum(train_seq_len_batch, axis=1), (2 if FLAGS.start_end else 1) * pad_width * ((train_seq_len_batch != 0).sum(axis=1) + (0 if FLAGS.start_end else 1))) for i, seq_len in enumerate(actual_seq_lens): mask_batch[i, :seq_len] = 1 train_batches.append( (train_label_batch, train_token_batch, train_shape_batch, train_char_batch, train_seq_len_batch, train_tok_len_batch, mask_batch)) except Exception as e: done = True if FLAGS.memmap_train: train_batcher.load_and_bucket_data(sess) def train(max_epochs, best_score, model_hidden_drop, model_input_drop, until_convergence, max_lower=6, min_iters=20): print("Training on %d sentences (%d examples)" % (num_train_examples, num_train_examples)) start_time = time.time() train_batcher._step = 1.0 converged = False examples = 0 log_every_running = log_every epoch_loss = 0.0 num_lower = 0 training_iteration = 0 speed_num = 0.0 speed_denom = 0.0 while not sv.should_stop( ) and training_iteration < max_epochs and not ( until_convergence and converged): # evaluate if examples >= num_train_examples: training_iteration += 1 if FLAGS.train_eval: run_evaluation( train_batches, "TRAIN (iteration %d)" % training_iteration) print() f1_micro, precision = run_evaluation( dev_batches, "TEST (iteration %d)" % training_iteration) print("Avg training speed: %f examples/second" % (speed_num / speed_denom)) # keep track of running best / convergence heuristic if f1_micro > best_score: best_score = f1_micro num_lower = 0 if FLAGS.model_dir != '' and best_score > FLAGS.save_min: save_path = saver.save(sess, FLAGS.model_dir + ".tf") print("Serialized model: %s" % save_path) else: num_lower += 1 if num_lower > max_lower and training_iteration > min_iters: converged = True # update per-epoch variables log_every_running = log_every examples = 0 epoch_loss = 0.0 start_time = time.time() if examples > log_every_running: speed_denom += time.time() - start_time speed_num += examples evaluation.print_training_error( examples, start_time, [epoch_loss], train_batcher._step) log_every_running += log_every # Training iteration label_batch, token_batch, shape_batch, char_batch, seq_len_batch, tok_lengths_batch = \ train_batcher.next_batch() if FLAGS.memmap_train else sess.run(train_batcher.next_batch_op) # make mask out of seq lens batch_size, batch_seq_len = token_batch.shape char_lens = np.sum(tok_lengths_batch, axis=1) max_char_len = np.max(tok_lengths_batch) padded_char_batch = np.zeros( (batch_size, max_char_len * batch_seq_len)) for b in range(batch_size): char_indices = [ item for sublist in [ range(i * max_char_len, i * max_char_len + d) for i, d in enumerate(tok_lengths_batch[b]) ] for item in sublist ] padded_char_batch[ b, char_indices] = char_batch[b][:char_lens[b]] max_sentences = max(map(len, seq_len_batch)) new_seq_len_batch = np.zeros((batch_size, max_sentences)) for i, seq_len_list in enumerate(seq_len_batch): new_seq_len_batch[i, :len(seq_len_list)] = seq_len_list seq_len_batch = new_seq_len_batch num_sentences_batch = np.sum(seq_len_batch != 0, axis=1) mask_batch = np.zeros( (batch_size, batch_seq_len)).astype("int") actual_seq_lens = np.add( np.sum(seq_len_batch, axis=1), (2 if FLAGS.start_end else 1) * pad_width * (num_sentences_batch + (0 if FLAGS.start_end else 1))).astype('int') for i, seq_len in enumerate(actual_seq_lens): mask_batch[i, :seq_len] = 1 examples += batch_size # apply word dropout # create word dropout mask word_probs = np.random.random(token_batch.shape) drop_indices = np.where( (word_probs > FLAGS.word_dropout) & (token_batch != vocab_str_id_map["<PAD>"])) token_batch[drop_indices[0], drop_indices[1]] = vocab_str_id_map["<OOV>"] char_embedding_feeds = {} if FLAGS.char_dim == 0 else { char_embedding_model.input_chars: padded_char_batch, char_embedding_model.batch_size: batch_size, char_embedding_model.max_seq_len: batch_seq_len, char_embedding_model.token_lengths: tok_lengths_batch, char_embedding_model.max_tok_len: max_char_len, char_embedding_model.input_dropout_keep_prob: FLAGS.char_input_dropout } if FLAGS.model == "cnn": cnn_feeds = { model.input_x1: token_batch, model.input_x2: shape_batch, model.input_y: label_batch, model.input_mask: mask_batch, model.max_seq_len: batch_seq_len, model.sequence_lengths: seq_len_batch, model.batch_size: batch_size, model.hidden_dropout_keep_prob: model_hidden_drop, model.input_dropout_keep_prob: model_input_drop, model.middle_dropout_keep_prob: FLAGS.middle_dropout, model.l2_penalty: FLAGS.l2, model.drop_penalty: FLAGS.regularize_drop_penalty, } cnn_feeds.update(char_embedding_feeds) _, loss = sess.run([train_op, model.loss], feed_dict=cnn_feeds) elif FLAGS.model == "bilstm": lstm_feed = { model.input_x1: token_batch, model.input_x2: shape_batch, model.input_y: label_batch, model.input_mask: mask_batch, model.sequence_lengths: seq_len_batch, model.max_seq_len: batch_seq_len, model.batch_size: batch_size, model.hidden_dropout_keep_prob: FLAGS.hidden_dropout, model.middle_dropout_keep_prob: FLAGS.middle_dropout, model.input_dropout_keep_prob: FLAGS.input_dropout, model.l2_penalty: FLAGS.l2, model.drop_penalty: FLAGS.regularize_drop_penalty } lstm_feed.update(char_embedding_feeds) _, loss = sess.run([train_op, model.loss], feed_dict=lstm_feed) epoch_loss += loss train_batcher._step += 1 return best_score, training_iteration, speed_num / speed_denom if FLAGS.evaluate_only: if FLAGS.train_eval: run_evaluation(train_batches, "(train)") print() run_evaluation(dev_batches, "(test)") if FLAGS.ontonotes: for domain, domain_batches in domain_batches.iteritems(): print() run_evaluation(domain_batches, FLAGS.layers2 != '', "(test - domain: %s)" % domain) else: best_score, training_iteration, train_speed = train( FLAGS.max_epochs, 0.0, FLAGS.hidden_dropout, FLAGS.input_dropout, until_convergence=FLAGS.until_convergence) if FLAGS.model_dir: print("Deserializing model: " + FLAGS.model_dir + ".tf") saver.restore(sess, FLAGS.model_dir + ".tf") sv.coord.request_stop() sv.coord.join(threads) sess.close() total_time = time.time() - training_start_time if FLAGS.evaluate_only: print("Testing time: %d seconds" % (total_time)) else: print( "Training time: %d minutes, %d iterations (%3.2f minutes/iteration)" % (total_time / 60, training_iteration, total_time / (60 * training_iteration))) print("Avg training speed: %f examples/second" % (train_speed)) print("Best dev F1: %2.2f" % (best_score * 100))
number_cpus = parameters["number_cpus"][0] likert_score_file_path = parameters["likert_scores_file_path"][0] loss_function = parameters["loss_function"][0] dataset_loader = DatasetLoader( labels_file_path=labels_file_path, input_folder_path=input_folder_path, likert_scores_file_path=likert_score_file_path) index = dataset_loader.get_index(psychological_construct) labels = dataset_loader.labels run_config = tf.ConfigProto(device_count={'CPU': number_cpus}) run_config.gpu_options.allow_growth = True with tf.Session(config=run_config) as sess: checkpoint_dir = parameters["checkpoint_dir"][0] checkpoint_dir = os.path.join(checkpoint_dir, psychological_construct) cnn_classifier = CNN(sess=sess, epochs=epochs, checkpoint_dir=checkpoint_dir, loss=loss_function) predicted_values = [] real_values = [] for student in labels.keys(): train_students = set(labels.keys()) - set([student]) test_student = set([student]) train_x, train_y = dataset_loader.get_x_and_y( students_set=train_students, index=index, test_flag=False) test_x, test_y = dataset_loader.get_x_and_y( students_set=test_student, index=index, test_flag=True) reshaped_train_set_x = dataset_loader.reshape_numpy_array(train_x) scaler = StandardScaler() scaler.fit(reshaped_train_set_x) normalized_reshaped_train_x = scaler.transform( reshaped_train_set_x)
import numpy as np from cnn import CNN import matplotlib.pyplot as plt code = 2 file_name = 'IL_weights_no_over' il_nn = CNN() history = il_nn.train(2 * 121) plt.plot(history[0]) plt.xlabel('Epoch') plt.ylabel('Cost') plt.title('Cost Function Convergence') plt.grid() plt.savefig('convergence_{}.png'.format(code), format='png') plt.figure() plt.plot(history[1]) plt.xlabel('Epoch') plt.ylabel('Val Acc') plt.title('Validation Accuracy Convergence') plt.grid() plt.savefig('validation_{}.png'.format(code), format='png') il_nn.save(file_name) # history = il_nn.evaluate() # print('evaluated_accuracy: {}'.format(history))
# -*- coding: utf-8 -*- from cnn import CNN import pandas as pd cnn = CNN() data = pd.DataFrame() data['cnn_feature'] = 0 data['cnn_feature'] = data['cnn_feature'].astype(object) featurelist = cnn.get_features([ '/home/seonhoon/Desktop/workspace/ImageQA/version_tensorflow/web/images/moodo.jpg' ], layer='fc7') data.loc[0, 'cnn_feature'] = featurelist[0].flatten() data.to_pickle( '/home/seonhoon/Desktop/workspace/ImageQA/version_tensorflow/web/cnn.pkl')
import matplotlib.pyplot as plt from cnn import data_utils from cnn.solver import Solver from cnn import CNN import numpy as np data = data_utils.get_CIFAR10_data() model = CNN.ThreeLayerConvNet(reg=0.9) solver = Solver(model, data, lr_decay=0.95, print_every=10, num_epochs=5, batch_size=8, update_rule='sgd_momentum', optim_config={ 'learning_rate': 5e-4, 'momentum': 0.9 }) solver.train() plt.subplot(2, 1, 1) plt.title('Training loss') plt.plot(solver.loss_history, 'o') plt.xlabel('Iteration') plt.subplot(2, 1, 2) plt.title('Accuracy') plt.plot(solver.train_acc_history, '-o', label='train') plt.plot(solver.val_acc_history, '-o', label='val')
from mlp import MLP from cnn import CNN from utils import read_binary_data, get_training_and_test_data import numpy as np x_data, y_data = read_binary_data('kp-sorted-dataset', 100, 100) (train_x, val_x, train_y, val_y) = get_training_and_test_data(x_data, y_data, percentage=0.6) cnn_model = CNN(descriptor_width=100, descriptor_height=100) scores = cnn_model.train(train_x, train_y, val_x, val_y, epochs=20, batch_size=16) print scores ''' cnn tried: --------- self.model.add(Convolution2D(8, 5, 5)) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(2, 2)) self.model.add(Convolution2D(16, 5, 5)) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(4, 4)) self.model.add(Flatten()) self.model.add(Dense(3500)) self.model.add(Activation('relu')) self.model.add(Dropout(0.2)) self.model.add(Dense(1500)) self.model.add(Activation('relu')) self.model.add(Dense(3)) self.model.add(Activation('softmax'))
#total_wrong += wrong idx += 1 acc = cal_acc(labels, results, total_ori_cand) timestr = datetime.datetime.now().isoformat() logging.info("%s, evaluation loss:%s, acc:%s"%(timestr, total_loss/idx, acc)) #logging.info("%s, evaluation loss:%s, acc:%s"%(timestr, total_loss/step, total_right/(total_right + total_wrong))) #---------------------------------- execute valid model end -------------------------------------- #----------------------------------- begin to train ----------------------------------- with tf.Graph().as_default(): with tf.device("/gpu:3"): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_options) session_conf = tf.ConfigProto(allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement, gpu_options=gpu_options) with tf.Session(config=session_conf).as_default() as sess: cnn = CNN(FLAGS.sequence_len, embedding, FLAGS.embedding_size, filter_sizes, FLAGS.num_filters) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(5e-2) #tf.train.GradientDescentOptimizer(1e-5) grads_and_vars = optimizer.compute_gradients(cnn.loss) train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step) sess.run(tf.initialize_all_variables()) #ori_quests, cand_quests = zip(*train_quests) #valid_ori_quests, valid_cand_quests = zip(*valid_quests) for ori_train, cand_train, neg_train in batch_iter(ori_quests, cand_quests, FLAGS.batch_size, FLAGS.epoches): run_step(sess, ori_train, cand_train, neg_train, cnn, FLAGS.dropout) cur_step = tf.train.global_step(sess, global_step)
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 204), 1) cv2.putText(image, CLASSES[prediction], (x, y), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 102), 1, CV_AA) return image test_image = cv2.imread(sys.argv[1]) face_rects = detect(test_image) face_images = get_face_images(test_image, face_rects) x = tf.placeholder(tf.float32, [None, PIXEL_COUNT]) labels = tf.placeholder(tf.float32, [None, len(CLASSES)]) keep_prob = tf.placeholder(tf.float32) cnn = CNN(image_size=FLAGS.image_size, class_count=len(CLASSES)) y = cnn.inference(x, keep_prob, softmax=True) sess = tf.InteractiveSession() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) saver.restore(sess, os.path.join(LOG_DIR, 'model.ckpt')) out_image = test_image.copy() for i in range(len(face_images)): face_image = face_images[i] softmax = sess.run(y, feed_dict={ x: [face_image], keep_prob: 1.0 }).flatten() prediction = np.argmax(softmax)
def fit_predict(data, labels, action, filename, test_datasets = [], learning_rate=0.1, n_epochs=20, nkerns=[20, 50], batch_size=500, seed=8000): rng = numpy.random.RandomState(seed) 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 index = T.lscalar() # index to a [mini]batch if action=='fit': NUM_TRAIN = len(data) if NUM_TRAIN % batch_size != 0: #if the last batch is not full, just don't use the remainder whole = (NUM_TRAIN / batch_size) * batch_size data = data[:whole] NUM_TRAIN = len(data) # random permutation indices = rng.permutation(NUM_TRAIN) data, labels = data[indices, :], labels[indices] # batch_size == 500, splits (480, 20). We will use 96% of the data for training, and the rest to validate the NN while training is_train = numpy.array( ([0]* (batch_size - 20) + [1] * 20) * (NUM_TRAIN / batch_size)) # now we split the dataset to test and valid datasets train_set_x, train_set_y = numpy.array(data[is_train==0]), labels[is_train==0] valid_set_x, valid_set_y = numpy.array(data[is_train==1]), labels[is_train==1] # compute number of minibatches n_train_batches = len(train_set_y) / batch_size n_valid_batches = len(valid_set_y) / batch_size ###################### # BUILD ACTUAL MODEL # ###################### print '... building the model' # allocate symbolic variables for the data epoch = T.scalar() #index = T.lscalar() # index to a [mini]batch #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 # construct the CNN class classifier = CNN( rng=rng, input=x, nkerns = nkerns, batch_size = batch_size ) train_set_x = theano.shared(numpy.asarray(train_set_x, dtype=theano.config.floatX)) train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y, dtype=theano.config.floatX)), 'int32') valid_set_x = theano.shared(numpy.asarray(valid_set_x, dtype=theano.config.floatX)) valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y, dtype=theano.config.floatX)), 'int32') validate_model = theano.function( inputs=[index], outputs=classifier.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] } ) cost = classifier.layer3.negative_log_likelihood(y) # create a list of gradients for all model parameters grads = T.grad(cost, classifier.params) # specify how to update the parameters of the model as a list of (variable, update expression) pairs updates = [ (param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(classifier.params, grads) ] # compiling a Theano function `train_model` that returns the cost, but # in the same time updates the parameter of the model based on the rules defined in `updates` train_model = theano.function( inputs=[index], outputs=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] } ) ############### # TRAIN MODEL # ############### print '... training' best_iter = 0 test_score = 0. start_time = time.clock() epoch = 0 # here is an example how to print the current value of a Theano variable: print test_set_x.shape.eval() # start training while (epoch < n_epochs): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): minibatch_avg_cost = train_model(minibatch_index) iter = (epoch - 1) * n_train_batches + minibatch_index if (epoch) % 5 == 0 and minibatch_index==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. ) ) ############### # PREDICTIONS # ############### # save and load f = file(filename, 'wb') cPickle.dump(classifier.__getstate__(), f, protocol=cPickle.HIGHEST_PROTOCOL) f.close() end_time = time.clock() print >> sys.stderr, ('The code ran for %.2fm' % ((end_time - start_time) / 60.)) if action == 'predict': # construct the CNN class classifier_2 = CNN( rng=rng, input=x, nkerns = nkerns, batch_size = batch_size ) print "...." f = file(filename, 'rb') classifier_2.__setstate__(cPickle.load(f)) f.close() RET = [] for it in range(len(test_datasets)): test_data = test_datasets[it] N = len(test_data) test_data = theano.shared(numpy.asarray(test_data, dtype=theano.config.floatX)) # just zeroes test_labels = T.cast(theano.shared(numpy.asarray(numpy.zeros(batch_size), dtype=theano.config.floatX)), 'int32') ppm = theano.function([index], classifier_2.layer3.pred_probs(), givens={ x: test_data[index * batch_size: (index + 1) * batch_size], y: test_labels }, on_unused_input='warn') # p : predictions, we need to take argmax, p is 3-dim: (# loop iterations x batch_size x 2) p = [ppm(ii) for ii in xrange( N / batch_size)] #p_one = sum(p, []) #print p p = numpy.array(p).reshape((N, 10)) print p p = numpy.argmax(p, axis=1) p = p.astype(int) RET.append(p) return RET
# Training # ================================================== with tf.Graph().as_default(): session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess = tf.Session(config=session_conf) with sess.as_default(): #embed() if (MODEL_TO_RUN == 0): model = CNN_LSTM(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim,filter_sizes,num_filters,l2_reg_lambda) elif (MODEL_TO_RUN == 1): model = LSTM_CNN(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim,filter_sizes,num_filters,l2_reg_lambda) elif (MODEL_TO_RUN == 2): model = CNN(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim,filter_sizes,num_filters,l2_reg_lambda) elif (MODEL_TO_RUN == 3): model = LSTM(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim) else: print ("PLEASE CHOOSE A VALID MODEL!\n0 = CNN_LSTM\n1 = LSTM_CNN\n2 = CNN\n3 = LSTM\n") exit(); # Define Training procedure global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(1e-3) grads_and_vars = optimizer.compute_gradients(model.loss) train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step) # Keep track of gradient values and sparsity (optional) grad_summaries = []
pre_train_lr = 0.1 pre_train_epoch = 10 # node_shape = ((80,52), (74,46), (35,21)) node_shape = ((80, 52), (74, 46), (34, 20)) filter_shift_list = ((1, 1), (2, 2)) input_shape = [80, 52] filter_shape = [7, 7] data_list = load_image(data_path, file_num, isRGB) makeFolder() # 時間計測 time1 = time.clock() cnn1 = CNN(data_list, filter_shape, filter_shift_list[0], input_shape, node_shape[1], pre_train_lr, pre_train_epoch) output_list = cnn1.output() saveImage(output_list, node_shape[1], 'cnn1_before_training') cnn1.pre_train() output_list = cnn1.output() saveImage(output_list, node_shape[1], 'cnn1_after_training') # for i in xrange(pre_train_epoch): # cnn1.pre_train() # output_list = cnn1.output() # saveImage(output_list, (74,46)) cnn2 = CNN(cnn1.output(), filter_shape, filter_shift_list[1], node_shape[1], node_shape[2], pre_train_lr, pre_train_epoch) output_list = cnn2.output()
def __init__(self): CNN.__init__( self, layers=[ ('image_input', layers.InputLayer), ('image_conv1', layers.Conv2DLayer), ('image_pool1', layers.MaxPool2DLayer), ('image_conv2', layers.Conv2DLayer), ('image_pool2', layers.MaxPool2DLayer), ('prob_input', layers.InputLayer), ('prob_conv1', layers.Conv2DLayer), ('prob_pool1', layers.MaxPool2DLayer), ('prob_conv2', layers.Conv2DLayer), ('prob_pool2', layers.MaxPool2DLayer), ('binary_input', layers.InputLayer), ('binary_conv1', layers.Conv2DLayer), ('binary_pool1', layers.MaxPool2DLayer), ('binary_conv2', layers.Conv2DLayer), ('binary_pool2', layers.MaxPool2DLayer), ('border_input', layers.InputLayer), ('border_conv1', layers.Conv2DLayer), ('border_pool1', layers.MaxPool2DLayer), ('border_conv2', layers.Conv2DLayer), ('border_pool2', layers.MaxPool2DLayer), ('merge', layers.ConcatLayer), ('hidden3', layers.DenseLayer), ('output', layers.DenseLayer), ], # input image_input_shape=(None, 1, 75, 75), # conv2d + pool + dropout image_conv1_filter_size=(13, 13), image_conv1_num_filters=16, image_conv1_nonlinearity=nonlinearities.rectify, image_pool1_pool_size=(2, 2), # conv2d + pool + dropout image_conv2_filter_size=(13, 13), image_conv2_num_filters=16, image_conv2_nonlinearity=nonlinearities.rectify, image_pool2_pool_size=(2, 2), prob_input_shape=(None, 1, 75, 75), # conv2d + pool + dropout prob_conv1_filter_size=(13, 13), prob_conv1_num_filters=16, prob_conv1_nonlinearity=nonlinearities.rectify, prob_pool1_pool_size=(2, 2), # conv2d + pool + dropout prob_conv2_filter_size=(13, 13), prob_conv2_num_filters=16, prob_conv2_nonlinearity=nonlinearities.rectify, prob_pool2_pool_size=(2, 2), binary_input_shape=(None, 1, 75, 75), # conv2d + pool + dropout binary_conv1_filter_size=(13, 13), binary_conv1_num_filters=16, binary_conv1_nonlinearity=nonlinearities.rectify, binary_pool1_pool_size=(2, 2), # conv2d + pool + dropout binary_conv2_filter_size=(13, 13), binary_conv2_num_filters=16, binary_conv2_nonlinearity=nonlinearities.rectify, binary_pool2_pool_size=(2, 2), border_input_shape=(None, 1, 75, 75), # conv2d + pool + dropout border_conv1_filter_size=(13, 13), border_conv1_num_filters=16, border_conv1_nonlinearity=nonlinearities.rectify, border_pool1_pool_size=(2, 2), # conv2d + pool + dropout border_conv2_filter_size=(13, 13), border_conv2_num_filters=16, border_conv2_nonlinearity=nonlinearities.rectify, border_pool2_pool_size=(2, 2), # concat merge_incomings=[ 'image_pool2', 'prob_pool2', 'binary_pool2', 'border_pool2' ], # dense layer 1 hidden3_num_units=256, hidden3_nonlinearity=nonlinearities.rectify, # dense layer 2 output_num_units=2, output_nonlinearity=nonlinearities.softmax)
batch_size = 10 input_shape = [3, 60, 40]; def random_matrix(shape, np_rng, name=None,type=floatX): return theano.shared(np.require(np_rng.randn(*shape), dtype=type), borrow=True, name=name) # define inputs and filters in_time = 7 in_channels, in_width, in_height = input_shape; flt_channels = 10 flt_time = 2 flt_width = 3 flt_height = 4 rng = np.random.RandomState(42) #(batch, row, column, time, in channel) train_x = random_matrix((batch_size, in_height, in_width, in_time, in_channels),rng, 'x'); train_y = random_matrix((batch_size,),rng,'y',type=np.dtype('int32')); valid_x = random_matrix((batch_size, in_height, in_width, in_time, in_channels),rng, 'x'); valid_y = random_matrix((batch_size,),rng,'y',type=np.dtype('int32')); numpy_rng = np.random.RandomState(89677) #theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) cnn = CNN(numpy_rng,batch_size=batch_size, n_outs=10,conv_layer_configs = conv_layer_configs, hidden_layer_configs=hidden_layer_configs); train_fn, validate_fn = cnn.build_finetune_functions((train_x,train_y), (valid_x,valid_y), batch_size=batch_size)
from data_provider import DataProvider from ck_data_provider import CK_DataProvider from fer2013_data_provider import FER2013_DataProvider np.set_printoptions(threshold=np.inf) LEARNING_RATE = 0.01 BATCH_SIZE = 50 EPOCH = 30 # WEIGHT_DECAY = 0.05 # data_provider = CK_DataProvider(BATCH_SIZE, 0) data_provider = FER2013_DataProvider(BATCH_SIZE, 0) cnn = CNN() cnn_model = TrainModel(data_provider, LEARNING_RATE, EPOCH) cnn_model.training(cnn, torch.optim.Adam(cnn.parameters(), lr=LEARNING_RATE), nn.CrossEntropyLoss()) test_output = cnn(data_provider.x_validation) pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() dummy_input = torch.tensor(np.expand_dims([np.random.rand(48, 48)], 1), dtype=torch.float32) torch.onnx.export(cnn, dummy_input, 'cnn_model.onnx', verbose=True) onnx_model = onnx.load('cnn_model.onnx') onnx.checker.check_model(onnx_model) print('Export cnn_model.onnx complete!')
# load the model to use for performance evaluation x = T.matrix('x') rng = numpy.random.RandomState(1234) # retrieve the project settings from the database project = DB.getProject('evalcnn') # create the model based on the project model = CNN( rng=rng, input=x, offline=True, batch_size=project.batchSize, patch_size=project.patchSize, nkerns=project.nKernels, kernel_sizes=project.kernelSizes, hidden_sizes=project.hiddenUnits, train_time=project.trainTime, momentum=project.momentum, learning_rate=project.learningRate, path=project.path_offline, id=project.id) #data = Data( project ) data = Data( project, offline=True, n_train_samples=700000, n_valid_samples=5000) model.train(data=data, offline=True, mean=project.mean, std=project.std) #print data.get_pixel_count(project)
# error checking if not opts.model_type in ["CNN", "MLP"]: raise ValueError(f"{opts.model_type} architecture not supported") # random seed for initializing weights if not opts.seed is None: torch.manual_seed(opts.seed) # getting data train_loader, valid_loader = load_data(batch_size=opts.batch_size) # both plots some sample data and gets the input size for the MLP input_size = None for images, labels in train_loader: input_size = (images.size(2), images.size(3)) if opts.plot: plot_mnist_images(images.squeeze(), labels, 18) plt.close() break # creating model2 num_classes = 10 model = CNN(num_classes) if opts.model_type == "CNN" else MLP(input_size, num_classes) # training model final_statistics = train(model, train_loader, valid_loader, opts) # saving model if opts.save: torch.save(model, f"{opts.model_type.lower()}.pt") print(f"model saved as {opts.model_type.lower()}.pt")
def __init__(self): super(CRNN, self).__init__() self.cnn = CNN() self.rnn = RNN(input_size=2048, hidden_size=256, output_size=4)
if __name__ == '__main__': model_name = 'bs_64_lr_0.001_run_85' model_path = f'../logs/{model_name}/model.pt' resize_transform = transforms.Resize((360, 640)) tensor_transform = transforms.ToTensor() image_path = f'../../train/0a0c3694-4cc8b0e3/0a0c3694-4cc8b0e3-8.png' image = Image.open(image_path) image_tensor = tensor_transform(resize_transform(image)).unsqueeze(0) model = CNN(640, 360, 3) model.load_state_dict( torch.load(model_path, map_location=torch.device('cpu'))) model.eval() # Visualize feature maps activation = {} def get_activation(name): def hook(model, input, output): activation[name] = output.detach() return hook model.conv1.register_forward_hook(get_activation('conv1'))