def load_pretrained_model(self, model_path, model_name): # Load model and dictionaries print("Loading model params...") params = load_params('%s/%s' % (model_path, model_name)) print("Loading dictionaries...") with open('%s/dict.pkl' % model_path, 'rb') as f: self.chardict = pkl.load(f) with open('%s/label_dict.pkl' % model_path, 'rb') as f: labeldict = pkl.load(f) self.n_char = len(self.chardict.keys()) + 1 n_classes = len(labeldict.keys()) print "#classes:", n_classes print labeldict print("Building network...") # Tweet variables tweet = T.itensor3() targets = T.imatrix() # masks t_mask = T.fmatrix() # network for prediction predictions = classify(tweet, t_mask, params, n_classes, self.n_char) # Theano function print("Compiling theano functions...") self.predict = theano.function([tweet, t_mask], predictions)
def classify_app(): # Write the POST method to post the result if request.method == 'POST': # Read article from textarea article = request.form.get('article') # Check if the article is not empty if article: # Get prediction of the article user_prediction = classify(article) # Convert the prediction from english to arabic if user_prediction[0] == 'Culture': user_prediction = 'تم تصنيف مقالتك على انها: مـقالـة ثـقـافـيـة' elif user_prediction[0] == 'Finance': user_prediction = 'تم تصنيف مقالتك على انها : مـقالـة إقـتـصـاديـة' elif user_prediction[0] == 'Medical': user_prediction = 'تم تصنيف مقالتك على انها : مـقالـة صـحـيـة' elif user_prediction[0] == 'Politics': user_prediction = 'تم تصنيف مقالتك على انها : مـقالـة سـيـاسـيـة' elif user_prediction[0] == 'Sports': user_prediction = 'تم تصنيف مقالتك على انها : مـقالـة ريـاضـيـة' elif user_prediction[0] == 'Tech': user_prediction = 'تم تصنيف مقالتك على انها : مـقالـة تـقـنـيـة' else: user_prediction = 'لا يـمـكـن تـحـديـده' # Return the result return jsonify(result=user_prediction) # If the article is empty else: # Return the error message return jsonify(result='الرجاء إدخال نص الخبر ') # Render the index template return render_template('index.html')
def input(): if request.method == 'GET': return render_template('input.html') else: img_file = request.files['img_file'] data_path = os.path.join(app.config['UPLOAD_FOLDER'], img_file.filename) img_file.save(data_path) result = inference.classify(data_path) return render_template('result.html', result=result, data_path=data_path)
def lambda_handler(event, context): if not event: return respond(ValueError('Invalid request - missing body')) print(event) if not event['data']: return respond(ValueError('Invalid request - missing data')) data = str(event['data']) if len(data) != 2500: return respond(ValueError('Invalid request - invalid data')) for char in data: if char not in "0123456789abcdef": return respond(ValueError('Invalid request - invalid data')) UID = str(int(dt.timestamp(dt.now())*10000)) payload = { 'TableName': 'catch-images', 'Item': { 'uid' : { 'S':UID }, 'device-id': { 'S':event['devicehash'] }, 'data': { 'S':event['data'] } } } # Attempt to classify drawing image = inference.unpack(event['data']) preds = inference.classify(image) dynamo.put_item(**payload) return respond(None, preds)
def hello_world(): # get page if request.method == 'GET': return render_template('index.html', value='hi') # send image if request.method == 'POST': print(request.files) if 'file' not in request.files: print('file not uploaded') return redirect(url_for('hello_world')) file = request.files['file'] # Save, resize, and resave image file.save("static/photos/original.jpg") image = Image.open('static/photos/original.jpg') my_transforms = transforms.Compose([transforms.Resize(256)]) image = my_transforms(image) image.save("static/photos/original.jpg") # Run image through classification function resultClass = classify(image) # Run image through Segmentation function resultSeg = segment(image) # Save Segmented image full_filename = os.path.join("/", app.config['UPLOAD_FOLDER'], 'file.jpg') resultSeg.convert('RGB').save("static/photos/segmention.jpg") # Redirect to result page with required information return redirect( url_for('getResult', resultClass=resultClass, resultSeg='segmention.jpg', original='original.jpg'))
def test(): img_class, gpu, img_type, net, probe_type, model = parse_arguments( sys.argv[1:]) os.environ["CUDA_VISIBLE_DEVICES"] = gpu probe_dir, gallery_dir = get_probe_gallery_dir(probe_type) y_ = tf.placeholder(tf.float32, [None, 2]) y = classify(get_feature_map(img_type, net)) accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y,1), \ tf.argmax(y_,1)), tf.float32)) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: saver = tf.train.Saver() ckpt = get_ckpt_file(img_class, net, probe_type) if ckpt and ckpt.model_checkpoint_path: print(ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] else: print("No checkpoint file found") return accs = [] test_view_list = [ "000", "018", "036", "054", "072", "090", "108", "126", "144", "162", "180" ] for view in test_view_list: test_x, test_y = load_data(img_class, "testing", view, probe_dir, gallery_dir) img0 = test_x[:, 0].reshape([-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) img1 = test_x[:, 1].reshape([-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) if img_type == "full": acc = sess.run(accuracy, feed_dict={probe_img:img0,\ gallery_img:img1, y_:test_y}) elif img_type == "partial": parted_probe_img_list = segment_batch_img(img0) parted_gallery_img_list = segment_batch_img(img1) acc = sess.run(accuracy, feed_dict={ parted_probe_img[0]: parted_probe_img_list[0], parted_probe_img[1]: parted_probe_img_list[1], parted_probe_img[2]: parted_probe_img_list[2], parted_probe_img[3]: parted_probe_img_list[3], parted_probe_img[4]: parted_probe_img_list[4], parted_probe_img[5]: parted_probe_img_list[5], parted_gallery_img[0]: parted_gallery_img_list[0], parted_gallery_img[1]: parted_gallery_img_list[1], parted_gallery_img[2]: parted_gallery_img_list[2], parted_gallery_img[3]: parted_gallery_img_list[3], parted_gallery_img[4]: parted_gallery_img_list[4], parted_gallery_img[5]: parted_gallery_img_list[5], y_: test_y }) elif img_type == "combined": pass else: print("Error: Wrong net type") sys.exit(1) accs.append(acc) print('After %s training steps, probe view:' % global_step, view,\ 'accuracy = %g' % acc) average_acc = sum(accs) / len(accs) print('After %s training steps,' % global_step, 'average accuracy = %g' % average_acc) x_axis = test_view_list y_axis = [100 * x for x in accs] plt.plot(x_axis, y_axis) plt.plot(x_axis, y_axis, 'ro') plt.xlabel('Probe view angle') plt.ylabel('Verification accuracy') if probe_type == 'NM': plt.title('Gallery: NM #1-4, view angles: 0-180 Probe: NM #5-6') elif probe_type == 'BG': plt.title('Gallery: NM #1-4, view angles: 0-180 Probe: BG #1-2') elif probe_type == 'CL': plt.title('Gallery: NM #1-4, view angles: 0-180 Probe: CL #1-2') else: print('Wrong probe type') plt.ylim(20, 100) plt.show()
def train(): ### parse arguments img_class, gpu, img_type, net, probe_type, model = parse_arguments( sys.argv[1:]) os.environ["CUDA_VISIBLE_DEVICES"] = '2' ### get probe and gallery dir probe_dir, gallery_dir = get_probe_gallery_dir(probe_type) ### angle train rfc_path = get_rfc_path(img_class, probe_type) if not os.path.exists(rfc_path): print("rfc model does not exist, need to train") rfc_model = RandomForestClassification() train_x, train_y = load_angle_train_data(img_class, view_list, probe_dir) rfc = rfc_model.fit(x_train=train_x, y_train=train_y) joblib.dump(rfc, rfc_path) ### cnn train y_ = tf.placeholder(tf.float32, [None, 2]) y = classify(get_feature_map(img_type, net)) cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(\ logits = y, labels = y_)) total_loss = cross_entropy + tf.add_n(tf.get_collection('losses')) accuracy = tf.reduce_mean( tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)), tf.float32)) train_step = tf.train.AdamOptimizer(0.0001).minimize(total_loss) #gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.45) gpu_options = tf.GPUOptions(allow_growth=True) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: saver = tf.train.Saver() paired_train_data = prepare_training_data(probe_dir, gallery_dir) NUM_BATCH = len(paired_train_data) // BATCH_SIZE sess.run(tf.global_variables_initializer()) x_axis = [] y_axis = [] y1_axis = [] start_step = 0 ckpt = get_ckpt_file(img_class, net, probe_type) if ckpt and ckpt.model_checkpoint_path: print(ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) start_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] n = 0 while (True): for i in range(NUM_BATCH): batch_x, batch_y = get_next_batch(img_class, probe_type, paired_train_data, BATCH_SIZE) batch_x0 = batch_x[:, 0, :, :].reshape( [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) batch_x1 = batch_x[:, 1, :, :].reshape( [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) if img_type == "full": _, loss = sess.run([train_step, total_loss], feed_dict={ probe_img: batch_x0, gallery_img: batch_x1, y_: batch_y }) elif img_type == "partial": parted_probe_img_list = segment_batch_img(batch_x0) parted_gallery_img_list = segment_batch_img(batch_x1) _, loss = sess.run( [train_step, total_loss], feed_dict={ y_: batch_y, parted_probe_img[0]: parted_probe_img_list[0], parted_probe_img[1]: parted_probe_img_list[1], parted_probe_img[2]: parted_probe_img_list[2], parted_probe_img[3]: parted_probe_img_list[3], parted_probe_img[4]: parted_probe_img_list[4], parted_probe_img[5]: parted_probe_img_list[5], parted_gallery_img[0]: parted_gallery_img_list[0], parted_gallery_img[1]: parted_gallery_img_list[1], parted_gallery_img[2]: parted_gallery_img_list[2], parted_gallery_img[3]: parted_gallery_img_list[3], parted_gallery_img[4]: parted_gallery_img_list[4], parted_gallery_img[5]: parted_gallery_img_list[5] }) elif img_type == "combined": pass else: print("Error: Wrong net type") sys.exit(1) global_step = int(start_step) + n * NUM_BATCH + i if (global_step % 10) == 0: print(global_step, " loss: ", loss) if (global_step % 100) == 0: accs = [] for view in view_list: val_x, val_y = load_data(img_class, "validation", view, probe_dir, gallery_dir) img0 = val_x[:, 0].reshape( [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) img1 = val_x[:, 1].reshape( [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) if img_type == "partial": parted_img0 = segment_batch_img(img0) parted_img1 = segment_batch_img(img1) acc = accuracy.eval({ parted_probe_img[0]: parted_img0[0], parted_probe_img[1]: parted_img0[1], parted_probe_img[2]: parted_img0[2], parted_probe_img[3]: parted_img0[3], parted_probe_img[4]: parted_img0[4], parted_probe_img[5]: parted_img0[5], parted_gallery_img[0]: parted_img1[0], parted_gallery_img[1]: parted_img1[1], parted_gallery_img[2]: parted_img1[2], parted_gallery_img[3]: parted_img1[3], parted_gallery_img[4]: parted_img1[4], parted_gallery_img[5]: parted_img1[5], y_: val_y }) elif img_type == "full": acc = accuracy.eval({ probe_img: img0, gallery_img: img1, y_: val_y }) elif img_type == "combined": pass else: print("Error: Wrong net type") sys.exit(1) accs.append(acc) avg_acc = sum(accs) / len(accs) x_axis.append(global_step) y_axis.append(avg_acc) y1_axis.append(loss) print(global_step, " accuracy: ", avg_acc) #if acc > 0.995 and n > 2: # saver.save(sess, './chpts/cnn_MT.model', \ # global_step=n*NUM_BATCH+i) # plt.plot(x_axis, y_axis) # plt.show() # sys.exit(0) if global_step != 0 and (global_step % 1000) == 0: saver.save(sess, get_ckpt_filename_to_save(img_class, net, probe_type),\ global_step=int(start_step)+n*NUM_BATCH+i) if global_step >= (int(start_step) + 25000): plt.figure(1) ax1 = plt.subplot(111) ax2 = plt.subplot(112) plt.sca(ax1) plt.plot(x_axis, y_axis, 'r', linewidth=2) plt.sca(ax2) plt.plot(x_axis, y1_axis, 'g', linewidth=2) plt.show() sys.exit(0) n += 1