Last active
March 10, 2021 20:42
-
-
Save Hammaad-M/68eb772ee13f84ea9bdf42793c5cf153 to your computer and use it in GitHub Desktop.
Source code from https://medium.com/@hammaad.memon "Creating an image-identifier from scratch with Tensorflow"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get packages | |
import tensorflow as tf | |
import numpy as np | |
from tensorflow import keras | |
# Load predefined data set | |
fashion_mnist = keras.datasets.fashion_mnist | |
# get data from data set | |
(training_images, training_labels), (test_images, test_labels) = fashion_mnist.load_data() | |
# Define neural net structure | |
# Sequential means that there a sequence of coloumns and that data must pass through each one | |
model = keras.Sequential([ | |
# (Step 1) input layer: 28x28 image | |
# "Flatten" extracts the 2d array into a single array of 784 characters | |
keras.layers.Flatten(input_shape=(28, 28)), | |
# (Step 3) hidden layer: 128 nodes long | |
# relu just gets rid of negative numbers at the hidden layer | |
keras.layers.Dense(units=128, activation=tf.nn.relu), | |
# (Step 2) output layer: 0-10 (a label for each possible clothing) | |
# ("Dense") means that every node is connected to every other parrallel node | |
# ("units=10") means that there are 10 possible outcomes | |
# ("activation=") takes an activation function which is a filtering mechanism within each node with specifies if data | |
# traveling through that layer is close enough to activate that node | |
# softmax simply maps the data to the node with the closest number | |
keras.layers.Dense(units=10, activation=tf.nn.softmax) | |
]) | |
# Compile the model; do some config and get it ready for training | |
# optimizer--biasly reduces chances of certain outputs returning false positives | |
# loss--represents the loss of accuracy | |
# using optimizers and loss functions specialized for images | |
model.compile(optimizer=tf.optimizers.Adam(), loss='sparse_categorical_crossentropy') | |
# Train model using the training data | |
# ("epochs=5") means analyze and optimize the model 5 times with the function | |
# specified above | |
model.fit( | |
training_images, | |
training_labels, | |
epochs=5 | |
) | |
# Get results from model | |
predictions = model.predict(test_images) | |
# process results and display | |
num_correct = 0 | |
for i in range(len(test_labels)): | |
if test_labels[i] == int(list(predictions[i]).index(max(predictions[i]))): | |
num_correct +=1 | |
num_incorrect = len(test_labels) - num_correct | |
print("correctly identified: " + str(num_correct) + " out of " + str(len(test_labels))) | |
print("total loss: " + str(num_incorrect)) | |
print("accuracy: " + str(1 - (num_incorrect/num_correct))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment