def predict(): if not request.json: flask.abort(400) data = {"success": False} imgbase64 = request.json['imgbase64'] model_typ = request.json['model'] start = time.process_time() if model_typ == 'cnn': lp, roi, rmb = cnn.predict(cnn_model,str(imgbase64)) if lp is not None: data['success'] = True data['lp'] = lp data['roi'] = roi data['rmb'] = rmb else: data['model'] = model_typ lp, roi, rmb = svm.predict(svm_model,str(imgbase64)) if lp is not None: data['success'] = True data['lp'] = lp data['roi'] = roi data['rmb'] = rmb end = time.process_time() - start data['time'] = end return jsonify(data)
def get_group_score(group, assignment_index, so_far, group_ind, groups_scores_map, traces): use_class_nn = True if groups_scores_map and tuple(group) in groups_scores_map: return groups_scores_map[tuple(group)] print "group: ", group group_traces = [traces[i] for i in group] image_file = plot_traces( group_traces, str(so_far) + '_' + str(assignment_index) + '_' + str(group_ind) + '_' + "group") if use_class_nn == False: image = Image.open(image_file).convert('L') image_tensor = pytorch_cnn.transform(image) prediction = pytorch_cnn.test_single_example(image_tensor, net, classes)[0] group_score = prediction[-1] else: with open('cseg_y_map', 'rb') as fp: y_map = cPickle.load(fp) with open('cseg_num_classes', 'rb') as fp: num_classes = cPickle.load(fp) with open('params.pkl', 'rb') as fp: params = cPickle.load(fp) array = np.asarray(Image.open(image_file).convert('L')) data = [array.flatten()] data = np.array(data, dtype=np.float128) prediction = cnn.predict(data, params, num_classes)[0] group_score = np.log(prediction[y_map['.NG']]) groups_scores_map[tuple(group)] = group_score print "group score: ", group_score return group_score
def predict(self, filename, expected_value): """ Input: filename - the temp file of the spoken word Output: bool - True if accuracy falls in range, False otherwise """ # wave, sr = librosa.load(filename, mono=True) # mfcc = librosa.feature.mfcc(wave, sr) # mfcc = np.pad(mfcc,((0,0),(0,100-len(mfcc[0]))), mode='constant', constant_values=0) # labels = [ # 'zero', # 'one', # 'two', # 'three', # 'four', # 'five', # 'six', # 'seven', # 'eight', # 'nine' # ] # test = np.reshape([mfcc], [-1, 20, 100]) # expected_index = labels.index(expected_value) # predicts = self.model.predict(test)[0] # accuracy = predicts[expected_index] # label = labels[expected_index] # print "I am {}% sure the word passed is {}".format(accuracy*100, label) accuracy = cnn.predict(filename, expected_value) return self.validate_accuracy(self.difficulty_level, accuracy)
def predict(): ret = request.json print(type(ret)) convertImage(ret) input = imread('output.png', mode='L') input = np.invert(input) input = zoom(input, 30.0 / sqrt(input.size)) input = np.delete(input, input.shape[0] - 1, 1) input = np.delete(input, 0, 1) input = np.delete(input, input.shape[0] - 1, 0) input = np.delete(input, 0, 0) input = input.reshape(1, 784) prediction, prob_array = cnn.predict(input) tf.reset_default_graph() return jsonify(results=[prediction, prob_array.tolist()])
def classify(self, gray, points): src_girds, girds = [], [] for point in points: x, y, w, h = point src_girds.append(gray[y:y + h, x:x + w]) tmp = 255 - gray[y:y + h, x:x + w] tmp = cv2.resize(tmp, (_size, _size)) _, tmp = cv2.threshold(tmp, Config.PRE_THRE, 255, 0) girds.append(tmp.reshape(-1) / 255) ret, proba = predict(girds) # this code is for check classify error # for src, gird, num, p in zip(src_girds, girds, ret, proba): # if num in (5, 3): # print(num, p) # plt.imshow(src) # plt.show() # plt.imshow(gird.reshape(_size, _size)) # plt.show() return ret, proba
def classify(): if request.method == 'POST': base64Data = request.get_json()['imgData'] data = re.sub('^data:image/.+;base64,', '', base64Data) img = Image.open(BytesIO(base64.b64decode(data))) img_size = 28, 28 img = img.resize(img_size, Image.ANTIALIAS) img = img.convert('L') img_array = np.asarray(img, dtype=np.float32) img_array = img_array.flatten() # convert to pytorch tensor img_tensor = torch.from_numpy(img_array) # divide image by its maximum pixel value for numerical stability img_tensor = img_tensor / torch.max(img_tensor) # [num_channel x image width x image height] img_tensor = img_tensor.view(1, 1, 28, 28) pred = predict(cnn, img_tensor) return '{ "number": %d}' % pred
plt.show() def resize_cards(test_images): resized_images = [] for img in test_images: width = 49 height = 101 dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) resized_images.append(resized) return resized_images test_images, test_labels = load_test_multiple_cards() model = load_trained_model() resized_cards = resize_cards(test_images) X_test = np.array(resized_cards) predicted_labels = predict(model, X_test) predicted_index = [] for label in predicted_labels: predicted_index.append(CLASSES.index(label)) score_accuracy = accuracy_score(test_labels, predicted_index) print(str(score_accuracy))
def cnnTest(testData): predicted_values= cnn.predict(testData) txtprint(predicted_values)
def segmentation_baseline_with_ocr(recordings, use_nn=True, use_cnn=False): import nn as nn import cnn as cnn import util import cPickle total_correct, total, total_correct_chars, total_chars = 0, 0, 0, 0 history = [] INTERSECTION_PROBABILITY_THRESHOLD = 0.8 if use_nn: # params, num_classes, y_map = cnn.main() # y_map, num_classes = cnn.main() # with open('cseg_y_map', 'w') as fp: # cPickle.dump(y_map, fp) # with open('cseg_num_classes', 'w') as fp: # cPickle.dump(num_classes, fp) with open('cseg_y_map', 'rb') as fp: y_map = cPickle.load(fp) with open('cseg_num_classes', 'rb') as fp: num_classes = cPickle.load(fp) params = {} with open('regularized_W1', 'rb') as fp: params['W1'] = cPickle.load(fp) with open('regularized_b1', 'rb') as fp: params['b1'] = cPickle.load(fp) with open('regularized_W2', 'rb') as fp: params['W2'] = cPickle.load(fp) with open('regularized_b2', 'rb') as fp: params['b2'] = cPickle.load(fp) inv_map = {v: k for k, v in y_map.iteritems()} ocr_used = 0 for so_far, hw in enumerate(recordings): strokes = hw.strokes pointlist = hw.get_pointlist() print "OVERLAP: File #{}".format(so_far) if hw == None: continue traces = literal_eval(hw.raw_data_json) # Get trace bounds trace_bounds = [] for t in traces: max_x, min_x, max_y, min_y = float('-inf'), float('+inf'), float( '-inf'), float('+inf') for d in t: y = d['y'] x = d['x'] if y > max_y: max_y = y elif y < min_y: min_y = y if x > max_x: max_x = x elif x < min_x: min_x = x trace_bounds.append((max_x, min_x, max_y, min_y)) # Determine groupings: seg_guess = [] current_group = [0] for i in range(1, len(trace_bounds)): for check_index in current_group: if i == check_index: continue if use_nn and abs(i - check_index) < 2: group = [pointlist[i], pointlist[check_index]] image_file = plot_traces( group, str(so_far) + '_' + str(i) + '_' + str(check_index)) array = np.asarray(Image.open(image_file).convert('L')) data = [array.flatten()] data = np.array(data, dtype=np.float128) prediction = cnn.predict(data, params, num_classes)[0] top_predictions = np.argsort(prediction)[::-1][:1] max_probability = prediction[top_predictions[0]] summary = {} if max_probability > INTERSECTION_PROBABILITY_THRESHOLD: print "value of i: {}, to_check: {}, OCR for segmentation".format( i, check_index) ocr_used += 1 current_group.append(i) break # print "************MAX PREDICTION: {} **** INTERSECTION PROBABILITIES: {}".format(max_prediction, summary) elif use_cnn and abs(i - check_index) < 2: group = [pointlist[i], pointlist[check_index]] image_file = plot_traces( group, str(so_far) + '_' + str(i) + '_' + str(check_index)) image = Image.open(image_file) image_tensor = pytorch_cnn.transform(image) net, classes = pytorch_cnn.main() prediction = pytorch_cnn.test_single_example( image_tensor, net, classes) top_five_predictions = np.argsort(prediction)[::-1][:1] max_probability = top_predictions[0] summary = {} if max_probability > INTERSECTION_PROBABILITY_THRESHOLD: for symbol in util.commonly_missegmented_symbols: if symbol not in y_map: continue id = y_map[symbol] if id in top_five_predictions: print "value of i: {}, to_check: {}, OCR for segmentation".format( i, check_index) ocr_used += 1 current_group.append(i) break else: seg_guess.append(current_group) current_group = [i] break if len(current_group) > 0: seg_guess.append(current_group) # print 'GUESS: ', seg_guess # print 'CORRECT: ', hw.segmentation for entry in range(len(pointlist)): entry_found = False for group in seg_guess: if entry in group: entry_found = True break if not entry_found: seg_guess.append([entry]) history.append((sorted(seg_guess, key=itemgetter(0)), hw.segmentation)) for g in seg_guess: if g in hw.segmentation: total_correct_chars += 1 total_chars += len(hw.segmentation) if array_equal(np.array(sorted(seg_guess, key=itemgetter(0))), np.array(hw.segmentation)): total_correct += 1 total += 1 for val in history: prediction, truth = val print "*****************************************************" print "Prediction: {}".format(prediction) print "Truth: {}".format(truth) print "*****************************************************" return total_correct, total, total_correct_chars, total_chars, ocr_used
def min_distance_segment( so_far, hw, nn_params=None, pytorch_params=None, use_nn=False, use_cnn=False, ): import nn as nn import cnn as cnn min_global = 5 strokes = hw.strokes pointlist = hw.get_pointlist() # print len(pointlist) neighbors = defaultdict(list) ocr_used = 0 INTERSECTION_PROBABILITY_THRESHOLD = 0.7 if use_nn: y_map = nn_params['y_map'] num_classes = nn_params['num_classes'] inv_map = nn_params['inv_map'] params = nn_params['params'] if use_cnn: y_map = pytorch_params['y_map'] num_classes = pytorch_params['num_classes'] inv_map = pytorch_params['inv_map'] for i, curr_trace in enumerate(strokes[:-1]): min_distance = float('inf') for j, other_trace in enumerate(strokes[i + 1:]): index = j + i + 1 distance = traceDistance(curr_trace, other_trace) if distance <= min_global: neighbors[i].append(index) elif use_nn and abs(i - index) < 2: group = [pointlist[i], pointlist[index]] image_file = plot_traces( group, str(so_far) + '_' + str(i) + '_' + str(index)) array = np.asarray(Image.open(image_file).convert('L')) data = [array.flatten()] data = np.array(data, dtype=np.float128) prediction = cnn.predict(data, params, num_classes)[0] max_prediction = inv_map[np.argmax(prediction)] top_predictions = np.argsort(prediction)[::-1][:1] max_probability = prediction[top_predictions[0]] summary = {} if max_probability > INTERSECTION_PROBABILITY_THRESHOLD: for symbol in util.commonly_missegmented_symbols: if symbol not in y_map: continue id = y_map[symbol] if id in top_predictions: print "value of i: {}, to_check: {}, OCR for segmentation".format( i, index) ocr_used += 1 neighbors[i].append(index) # print "************MAX PREDICTION: {} **** INTERSECTION PROBABILITIES: {}".format(max_prediction, summary) elif use_cnn: group = [pointlist[i], pointlist[index]] image_file = plot_traces( group, str(so_far) + '_' + str(i) + '_' + str(index)) image = Image.open(image_file) image_tensor = pytorch_cnn.transform(image) net, classes = pytorch_cnn.main() outputs = pytorch_cnn.test_single_example( image_tensor, net, classes) for position, prob in enumerate(outputs): symbol = inv_map[position] if symbol in util.commonly_missegmented_symbols and prob > INTERSECTION_PROBABILITY_THRESHOLD: print "value of i: {}, to_check: {}, OCR for segmentation".format( i, index) ocr_used += 1 neighbors[i].append(index) continue groups = [] values = list(itertools.chain.from_iterable(neighbors.values())) neighbors_copy = {} seen = set() for key, vals in neighbors.iteritems(): if key in seen: continue neighbors_copy[key] = vals for val in vals: if val in neighbors: neighbors_copy[key] = list( set(neighbors_copy[key] + neighbors[val])) seen.add(val) neighbors = neighbors_copy for k in range(len(pointlist)): if k not in neighbors.keys() and k not in values: groups.append([k]) for key, val in neighbors.iteritems(): group = [key] + val groups.append(group) groups = sorted(groups, key=itemgetter(0)) return groups, ocr_used
def evaluate(test_num, test_tfrecord_file, test_pred_file): """Eval Multi-task_cnn for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for Multi-task_cnn. images, skuid, labels, hots = cnn.inputs(test_tfrecord_file, eval_data=True, batch_size=FLAGS.test_batch_size) # Build a Graph that computes the logits predictions from the # inference model. logits = cnn.inference(images, n_cnn=5) hots = tf.cast(hots, tf.float32) logits = tf.multiply(logits, hots, name='assign_label') num_splits = tf.constant(cnn.obtain_splits(num_splits_path)) # Calculate predictions. cnn_pred = cnn.predict(logits, num_splits) origin_pred = cnn.predict(labels, num_splits) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cnn.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) print("restore from file") global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] else: print('No checkpoint file found') return # Start the queue runners. coord = tf.train.Coordinator() try: threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend(qr.create_threads(sess, coord=coord, daemon=True, start=True)) num_iter = int(math.ceil(test_num / FLAGS.test_batch_size)) # Compute precision @ 1. sku, cnn_predi, origin_predi = sess.run([skuid, cnn_pred, origin_pred]) record_sku_pred(sku, cnn_predi, origin_predi, num_splits, test_pred_file, 'wb') step = 0 while step < num_iter and not coord.should_stop(): sku, cnn_predi, origin_predi = sess.run([skuid, cnn_pred, origin_pred]) record_sku_pred(sku, cnn_predi, origin_predi, num_splits, test_pred_file, 'ab') step += 1 summary = tf.Summary() summary.ParseFromString(sess.run(summary_op)) # summary.value.add(tag='Precision @ 1', simple_value=precision) summary_writer.add_summary(summary, global_step) except Exception as e: # pylint: disable=broad-except coord.request_stop(e) coord.request_stop() coord.join(threads, stop_grace_period_secs=10)
import cv2 import sys import os import cnn for img_path in sys.argv[1:]: if (os.path.isfile(img_path) == False): try: raise FileNotFoundError('no such file') except FileNotFoundError: raise image = cv2.imread(img_path, 0) di_path = "./traind_model/" cnn_model = cnn.cnn(40, di_path + "model_40px") cnn.predict(image)
print(teerror) utils.plot_2dclassifier(model, Xtest, ytest) # part 1: implement knn.predict # part 2: print training and test errors for k=1,3,10 (use utils.classification_error) # part 3: plot classification boundaries for k=1 (use utils.plot_2dclassifier) if question == '1.2': dataset = utils.load_dataset('citiesBig1') X = dataset['X'] y = dataset['y'] Xtest = dataset['Xtest'] ytest = dataset['ytest'] model = cnn.fit(X, y, 1) y_pred_tr = cnn.predict(model, X) y_pred_te = cnn.predict(model, Xtest) trerror = utils.classification_error(y_pred_tr, y) teerror = utils.classification_error(y_pred_te, ytest) print(trerror) print(teerror) utils.plot_2dclassifier(model, X, y) # part 1: implement cnn.py # part 2: print training/test errors as well as number of examples for k=1 # part 3: plot classification boundaries for k=1 if question == '2.1': dataset = utils.load_dataset('vowel') X = dataset['X']
def result(): name = request.form['name'] res = predict(name) return render_template('final.html', res=res)