nn-conv2d.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import pandas as pd
  2. import numpy
  3. import tensorflow as tf
  4. from tensorflow.keras import datasets, layers, models
  5. import math
  6. import time
  7. import tkinter
  8. from matplotlib import pyplot as plt
  9. # Read train data from csv
  10. df = pd.read_csv('train.csv')
  11. # Shuffle data after read
  12. df = df.sample(frac=1)
  13. # Drop and separate label from the data
  14. df_label = df['label']
  15. del df['label']
  16. # Convert both dataframes to numpy objects
  17. X = df.to_numpy()
  18. y = df_label.to_numpy()
  19. # Reshape and resize data (80% train data, 20% validation data)
  20. X = X.reshape((-1, 28, 28, 1))
  21. X_train_size = math.floor(len(X)*.8)
  22. X_test_size = len(X) - X_train_size
  23. X_train = X[:X_train_size]
  24. X_test = X[X_test_size+1:]
  25. y_train = y[:X_train_size]
  26. y_test = y[X_test_size+1:]
  27. # One Hot Encode the Label
  28. y_train = numpy.eye(10)[y_train]
  29. y_test = numpy.eye(10)[y_test]
  30. # Normalize the Input between 0 and 1
  31. X_train = X_train/255
  32. X_test = X_test/255
  33. # Create the convolutional neural network model
  34. model = models.Sequential()
  35. model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
  36. model.add(layers.MaxPooling2D((2, 2)))
  37. model.add(layers.Conv2D(64, (3, 3), activation='relu'))
  38. model.add(layers.MaxPooling2D((2, 2)))
  39. model.add(layers.Conv2D(128, (3, 3), activation='relu'))
  40. model.add(layers.Flatten())
  41. model.add(layers.Dense(64, activation='relu'))
  42. model.add(layers.Dense(10, activation='softmax'))
  43. # Define adam as the optmizer and MSE as the loss function
  44. optmizer = tf.keras.optimizers.Adam(learning_rate=0.001)
  45. model.compile(optimizer=optmizer, loss=tf.keras.losses.MeanSquaredError(),
  46. metrics=['accuracy'])
  47. # Train the model
  48. history = model.fit(X_train, y_train, batch_size=2**12,
  49. epochs=30, validation_data=(X_test, y_test))
  50. # Load test dataset as pandas dataframe and convert to numpy
  51. df = pd.read_csv('test.csv')
  52. X = df.to_numpy()
  53. # Reshape the data for the expected format of the model and predict the output
  54. y = model.predict(X.reshape(-1, 28, 28, 1))
  55. # For each input, save to file the predicted output
  56. with open('submission.csv', 'w') as f:
  57. f.write("ImageId,Label\n")
  58. idx = 1
  59. for i in y:
  60. f.write(f"{idx},{numpy.argmax(i)}\n")
  61. idx += 1