Skip to content

Instantly share code, notes, and snippets.

View frenzy2106's full-sized avatar

Ankit Choudhary frenzy2106

View GitHub Profile
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Import numpy, Pandas, Regex & Visualisation libraries
import numpy as np
import pandas as pd
import re
from matplotlib import pyplot as plt
# unzip the test file to read images
!unzip /content/drive/My\ Drive/test_ScVgIM0.zip
# Read test file names
test = pd.read_csv('test.csv')
test_copy = test.copy()
# Read test images and preprocess
test_image = []
for i in tqdm(range(test.shape[0])):
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
# Create Train and validation data to check the performance at each epoch
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
# Using Keras Sequential API to add neural network layers
model = keras.Sequential()
model.add(keras.layers.Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(28,28,1)))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Flatten())
# We have grayscale images, so while loading the images we will keep grayscale=True, if you have RGB images, you should set grayscale as False
train_image = []
for i in tqdm(range(train.shape[0])):
img = image.load_img('train/'+train['id'][i].astype('str')+'.png', target_size=(28,28,1), color_mode="grayscale")
img = image.img_to_array(img)
img = img/255
train_image.append(img)
X = np.array(train_image)
# Preprocessing the Target
!unzip /content/drive/My\ Drive/train_LbELtWX.zip
train = pd.read_csv('train.csv')
from google.colab import drive
drive.mount('/content/drive')
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from keras.preprocessing import image
@frenzy2106
frenzy2106 / tf_2-x.py
Created March 17, 2020 10:23
Ensure only TensorFlow 2.x is used
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
@frenzy2106
frenzy2106 / tensorflow_1_example.py
Created February 17, 2020 12:11
Tensorflow 1.x example
import tensorflow as tf
x = tf.constant([1,2,3,4,5])
y = tf.constant([1,1,1,1,1])
a = tf.add(x,y)
print(a)