4 examples of 'keras model predict' in Python

Every line of 'keras model predict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
15def _predict(self, X, **kwargs):
16 '''
17 Keras returns class tuples (proba equivalent) so cast to single prediction
18 '''
19 return np.argmax(self.external_model.predict(X), axis=1)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

31def predict(args):
32 # load the trained convolutional neural network
33 print("[INFO] loading network...")
34 model = load_model(args["model"])
35 stride = args['stride']
36 for n in range(len(TEST_SET)):
37 path = TEST_SET[n]
38 #load the image
39 image = cv2.imread('./test/' + path)
40 # pre-process the image for classification
41 #image = image.astype("float") / 255.0
42 #image = img_to_array(image)
43 h,w,_ = image.shape
44 padding_h = (h//stride + 1) * stride
45 padding_w = (w//stride + 1) * stride
46 padding_img = np.zeros((padding_h,padding_w,3),dtype=np.uint8)
47 padding_img[0:h,0:w,:] = image[:,:,:]
48 padding_img = padding_img.astype("float") / 255.0
49 padding_img = img_to_array(padding_img)
50 print 'src:',padding_img.shape
51 mask_whole = np.zeros((padding_h,padding_w),dtype=np.uint8)
52 for i in range(padding_h//stride):
53 for j in range(padding_w//stride):
54 crop = padding_img[:3,i*stride:i*stride+image_size,j*stride:j*stride+image_size]
55 _,ch,cw = crop.shape
56 if ch != 256 or cw != 256:
57 print 'invalid size!'
58 continue
59
60 crop = np.expand_dims(crop, axis=0)
61 #print 'crop:',crop.shape
62 pred = model.predict_classes(crop,verbose=2)
63 pred = labelencoder.inverse_transform(pred[0])
64 #print (np.unique(pred))
65 pred = pred.reshape((256,256)).astype(np.uint8)
66 #print 'pred:',pred.shape
67 mask_whole[i*stride:i*stride+image_size,j*stride:j*stride+image_size] = pred[:,:]
68
69
70 cv2.imwrite('./predict/pre'+str(n+1)+'.png',mask_whole[0:h,0:w])
47@abstractmethod
48def predict(self, x_test):
49 """Return the results for the testing data predicted by the best neural architecture.
50
51 Dependent on the results of the fit() function.
52
53 Args:
54 x_test: An instance of numpy.ndarray containing the testing data.
55
56 Returns:
57 A numpy.ndarray containing the predicted classes for x_test.
58 """
59 pass
372def predict(self, x, batch_size=None, verbose=0, steps=None):
373 target_bounding_boxes = numpy.zeros((x.shape[0], 1, 4))
374
375 target_categories = numpy.zeros((x.shape[0], 1, self.n_categories))
376
377 target_mask = numpy.zeros((1, 1, *self.mask_shape))
378
379 target_metadata = numpy.array([[x.shape[1], x.shape[2], 1.0]])
380
381 x = [
382 target_bounding_boxes,
383 target_categories,
384 x,
385 target_mask,
386 target_metadata
387 ]
388
389 return super(RCNN, self).predict(x, batch_size, verbose, steps)

Related snippets