Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LETTER_IMAGES_FOLDER = "datasets"
- MODEL_FILENAME = "fonts_model.hdf5"
- MODEL_LABELS_FILENAME = "model_labels.dat"
- data = pd.read_csv('annotations.csv')
- paths = list(data['Path'].values)
- Y = list(data['Font'].values)
- encoder = LabelEncoder()
- encoder.fit(Y)
- Y = encoder.transform(Y)
- Y = np_utils.to_categorical(Y)
- data = []
- # loop over the input images
- for image_file in paths:
- # Load the image and convert it to grayscale
- image = cv2.imread(image_file)
- image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # Add a third channel dimension to the image to make Keras happy
- image = np.expand_dims(image, axis=2)
- # Add the letter image and it's label to our training data
- data.append(image)
- data = np.array(data, dtype="float") / 255.0
- train_x, test_x, train_y, test_y = model_selection.train_test_split(data,Y,test_size = 0.1, random_state = 0)
- # Save the mapping from labels to one-hot encodings.
- # We'll need this later when we use the model to decode what it's predictions mean
- with open(MODEL_LABELS_FILENAME, "wb") as f:
- pickle.dump(encoder, f)
- # Build the neural network!
- model = Sequential()
- # First convolutional layer with max pooling
- model.add(Conv2D(20, (5, 5), padding="same", input_shape=(100, 100, 1), activation="relu"))
- model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
- # Second convolutional layer with max pooling
- model.add(Conv2D(50, (5, 5), padding="same", activation="relu"))
- model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
- model.add(Flatten())
- model.add(Dense(500, activation="relu"))
- print (len(encoder.classes_))
- model.add(Dense(len(encoder.classes_), activation="softmax"))
- # Ask Keras to build the TensorFlow model behind the scenes
- model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
- # Train the neural network
- model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=2, verbose=1)
- # Save the trained model to disk
- model.save(MODEL_FILENAME)
- predictions = model.predict(letter_image)
- print (predictions) # this has the length of 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement