opencv - Simple Neural Network in Python not displaying label for the test image -
i followed tutorial learn how create simple neural network using python. below code:
def image_to_feature_vector(image, size=(32,32)): return cv2.resize(image, size).flatten() ap = argparse.argumentparser() ap.add_argument("-d", "--dataset", required=true, help="path input dataset") args = vars(ap.parse_args()) print("[info] describing images...") imagepaths = list(paths.list_images(args["dataset"])) print(imagepaths) #this list of image paths # initialize data matrix , labels list data = [] labels = [] (i, imagepath) in enumerate(imagepaths): image = cv2.imread(imagepath) label = imagepath.split(os.path.sep)[-1].split(".")[0] features = image_to_feature_vector(image) data.append(features) labels.append(label) # show update every 1,000 images if > 0 , % 1000 == 0: print("[info] processed {}/{}".format(i, len(imagepaths))) # encode labels, converting them strings integers le = labelencoder() labels = le.fit_transform(labels) data = np.array(data) / 255.0 labels = np_utils.to_categorical(labels, 2) print("[info] constructing training/testing split...") (traindata, testdata, trainlabels, testlabels) = train_test_split( data, labels, test_size=0.25, random_state=42) #constructing neural network model = sequential() model.add(dense(768, input_dim=3072, init="uniform", activation="relu")) model.add(dense(384, init="uniform", activation="relu")) model.add(dense(2)) model.add(activation("softmax")) # train model using sgd print("[info] compiling model...") sgd = sgd(lr=0.01) model.compile(loss="binary_crossentropy", optimizer=sgd, metrics=["accuracy"]) model.fit(traindata, trainlabels, nb_epoch=50, batch_size=128) #test model # show accuracy on testing set print("[info] evaluating on testing set...") (loss, accuracy) = model.evaluate(testdata, testlabels, batch_size=128, verbose=1) print("[info] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))
the last few lines run trained neural network against testing set , displays accuracy follows:
but, there way instead of testing set, supply path of image , tells whether cat or dog (this tutorial used cat/dog sample, using now). how do in above code? thanks.
keras models have predict method .
predictions = model.predict(images_as_numpy_array)
will give predictions on chosen data. have opened , transformed image numpy array. did training , testing set following lines:
image = cv2.imread(imagepath) label = imagepath.split(os.path.sep)[-1].split(".")[0] features = image_to_feature_vector(image)
Comments
Post a Comment