def model_predict(img1): #loads trained model model = train_model.model() test_data = cv2.resize(cv2.imread(img1), (100, 100)) cv2.imwrite(img1 + '_resized.jpg', test_data) np.shape(test_data) model_out = model.predict([test_data]) x = np.reshape(model_out, (100, 100, 3)) cv2.imwrite(img1 + '_output.jpg', x)
def model_predict(img1): #loads trained model print('model loading') model = train_model.model() #if img1 is not None: print('model load completed') test_data = cv2.resize( cv2.imread( "/home/gopika/Project/Sketch-Photo-Conversion-using-Deep-CNN-master/Main Project/static/img/sketch.jpg" ), (100, 100)) cv2.imwrite( '/home/gopika/Project/Sketch-Photo-Conversion-using-Deep-CNN-master/Main Project/static/img/sketch' + '_resized.jpg', test_data) np.shape(test_data) model_out = model.predict([test_data]) x = np.reshape(model_out, (100, 100, 3)) cv2.imwrite( '/home/gopika/Project/Sketch-Photo-Conversion-using-Deep-CNN-master/Main Project/static/img/output/output.jpg', x)
def get_keys_from_ad(ad_path): image = Image.open(ad_path) image = image.convert('RGB') #make transforms image = functional.to_tensor(image) image = functional.normalize(image, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) image = image.unsqueeze(0) #call model to modify the image here #output of the image is a numpy array(nd array) model.eval() with torch.no_grad(): #out = model(image.cuda()) out = model(image.cpu()) nd_out = out.clone() nd_out = nd_out.detach().cpu().squeeze(0).numpy().transpose(1, 2, 0) # plot_grayscale_image(image.squeeze(0)) # plot_numpy_image(nd_out) tesser_out = read_keys(nd_out) #print(tesser_out) return [ad_path, str(tesser_out)]
from train_model import model x, y, test_data, vocabulary = data_reader.load_data() x_train, x_val = np.concatenate((x[:26319],x[:26319],x[:26319],x[:26319],x[:26319],x[:26319],x[:26319],x[:26319],x[:26319],x[:26319], x[26349:-300])), np.concatenate((x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349],x[26319:26349], x[-300:])) y_train, y_val = np.concatenate((y[:26319],y[:26319],y[:26319],y[:26319],y[:26319],y[:26319],y[:26319],y[:26319],y[:26319],y[:26319], y[26349:-300])), np.concatenate((y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349],y[26319:26349], y[-300:])) _index = np.random.permutation(np.arange(len(x_train))) x_train = x_train[_index] y_train = y_train[_index] article_length = x_train.shape[1] out_dir = "logs/" sess = tf.Session() with sess.as_default(): model_ = model(article_length=article_length,vocab_size=len(vocabulary)) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(2e-4) train_op = optimizer.apply_gradients(optimizer.compute_gradients(model_.loss), global_step=global_step) saver = tf.train.Saver(tf.all_variables()) sess.run(tf.initialize_all_variables()) ckpt = tf.train.get_checkpoint_state(os.path.join(out_dir, 'checkpoints')) def eval(x_batch, y_batch): feed_dict = {model_.train_articles: x_batch, model_.dropout: 1.0} ans1 = sess.run([model_.pred_score], feed_dict) auc = roc_auc_score(y_batch,ans1[0]) if(auc > 0.92):
def main(): #loading pre-trained model or train model if model does not exist model = train_model.model() cap = cv2.VideoCapture(0) #cap.set(6, 10) faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, 1.3, 15) for (x,y,h,w) in faces: roi_gray = gray[y:y+h+25, x:x+w+25] roi_color = frame[y:y+h+25, x:x+w+25] #grayscale video frame = cv2.cvtColor(roi_color, cv2.COLOR_BGR2GRAY) img_gray = np.dstack([frame, frame, frame]) #converts to sketch img_gray_inv = 255 - img_gray img_blur = cv2.GaussianBlur(img_gray_inv, ksize=(21, 21), sigmaX=0, sigmaY=0) def dodgeV2(image, mask): return cv2.divide(image, 255-mask, scale=256) img_blend = dodgeV2(img_gray, img_blur) test_data = cv2.resize(img_blend, (100,100)) #img_blend = cv2.resize(img_blend, (100,100)) photo = cv2.resize(roi_color, (100,100)) cv2.imshow('frame', test_data) #cv2.imshow('frame2', photo) #output prediction of model model_out = model.predict([test_data]) #normalize frame output x = np.reshape(model_out, (100,100,3)) maxVal = max(x.flatten()); minVal = min(x.flatten()); Img = (((x.flatten() - minVal) / ( maxVal - minVal))) x = np.reshape(Img, (100,100,3)) cv2.imshow('frame3', x) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
import train_model import DataPreprocessing hidden_layer_dims = [7, 8] layer_types = ['relu', 'relu', 'sigmoid'] learning_rate = 0.001 num_iterations = 1000 # num_batches = 6 lambd = 0 prob = 1 threshold = 0.5 X_train_scaled, X_test_scaled, Y_train, Y_test = DataPreprocessing.opendata( "wine.csv", "DNN") X_train_tr = X_train_scaled.T Y_train_tr = Y_train.T Y_test = Y_test.T X_test_scaled = X_test_scaled.T params = train_model.model(X_train_tr, Y_train_tr, hidden_layer_dims, layer_types, learning_rate, num_iterations, prob) yptest, accuracytest = train_model.predict(X_test_scaled, Y_test, params, hidden_layer_dims, layer_types, 0.5) print('The accuracy of training set is: %d' % accuracytest) yptrain, accuracytrain = train_model.predict(X_train_tr, Y_train_tr, params, hidden_layer_dims, layer_types, 0.5) print('The accuracy of testing set is: %d' % accuracytrain)
model.eval() rospy.init_node('drive_by_model', anonymous=True) robot = Robot('thymio') while True: # read image from camera and do transform image = robot.read_camera() image = Image.fromarray(image) image = tsfm(image) image = image.unsqueeze(0) # perdict labels predicts = model(image).detach().reshape(-1) T_predict = predicts[0] C_predict = predicts[1] # calculate real labels for comparing sensor_vector = robot.read_sensor() T_real = np.dot(sensor_vector, [1, 2, 0, -2, -1])/3 C_real = np.dot(sensor_vector, [-1, -1, 4, -1, -1])/4 rospy.loginfo('The predict T is {:.4f}, the real T is {:.4f},\n The predict C is {:.4f}, The real C is {:.4f}'.format(T_predict, T_real, C_predict, C_real)) # drive the robot using predict labels ang_vel = T_predict * 6 * pi lin_vel = (1 - abs(C_predict))*0.2