Kaggle competition: Digit Recognizer
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 device<load_from_csv>
print(tf.__version__ )
data_path = '/kaggle/input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(data_path + 'train.csv')
 test = pd.read_csv(data_path + 'test.csv')
 submission = pd.read_csv(data_path + 'sample_submission.csv' )<split>
train=pd.read_csv(".. /input/digit-recognizer/train.csv")
 test=pd.read_csv(".. /input/digit-recognizer/test.csv")
 print("Train Shape: {}".format(train.shape))
print("Test Shape: {}".format(test.shape))
train, valid = train_test_split(train,
test_size=0.1,
stratify=train[['healthy', 'multiple_diseases', 'rust', 'scab']],
random_state=50 )<normalization>
print("Train Nulls: {}".format(train.isna().any().sum()))
print("Test Nulls: {}".format(test.isna().any().sum()))
class ImageDataset(Dataset):
def __init__(self, df, img_dir='./', transform=None, is_test=False):
super().__init__()
self.df = df
self.img_dir = img_dir
self.transform = transform
self.is_test = is_test
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
img_id = self.df.iloc[idx, 0]
img_path = self.img_dir + img_id + '.jpg'
image = cv2.imread(img_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image=image)['image']
if self.is_test:
return image
else:
label = np.argmax(self.df.iloc[idx, 1:5])
 return image, label<import_modules>
y = train['label'].astype('int8')
 X = train.drop(columns=['label'] ).astype('float16' ).values
X = X.reshape(42000, 28,28, 1 )
import albumentations as A
from albumentations.pytorch import ToTensorV2<categorify>
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.2 )
transform_test = A.Compose([
A.Resize(450, 650),
A.Normalize() ,
ToTensorV2()
] )<create_dataframe>
train_image_generator = ImageDataGenerator(
 rescale=1./255 
)
 validation_image_generator = ImageDataGenerator(
 rescale=1./255 
)
 train_image_gen = train_image_generator.flow(
 x=X_train,
y=y_train, 
)
 validation_image_gen = validation_image_generator.flow(
 x=X_validation,
y=y_validation,
)
img_dir = '/kaggle/input/plant-pathology-2020-fgvc7/images/'
dataset_train = ImageDataset(train, img_dir=img_dir, transform=transform_train)
 dataset_valid = ImageDataset(valid, img_dir=img_dir, transform=transform_test )<define_variables>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3,3), activation='relu',padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(128,(3,3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(( 2,2)) ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(64,(3,3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(128,(3,3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(( 2,2)) ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(10, activation='softmax')
 ] )
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
 random.seed(worker_seed)
 g = torch.Generator()
g.manual_seed(0 )<load_pretrained>
tf.keras.backend.clear_session()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(
 train_image_gen,
epochs=5,
validation_data=validation_image_gen
)
batch_size = 4
loader_train = DataLoader(dataset_train, batch_size=batch_size,
shuffle=True, worker_init_fn=seed_worker,
generator=g, num_workers=2)
 loader_valid = DataLoader(dataset_valid, batch_size=batch_size,
shuffle=False, worker_init_fn=seed_worker,
generator=g, num_workers=2 )<install_modules>
validation_predict = model.predict(X_validation)
 low_predictions = pd.DataFrame()
low_predictions['label'] = np.argmax(validation_predict, axis=1)
 low_predictions['Confidence'] = np.max(validation_predict, axis=1)
 low_index = low_predictions.sort_values(by=['Confidence'])[:10].index
low_labels = low_predictions.sort_values(by=['Confidence'])['label']
fig, axs = plt.subplots(2, 5, figsize=(24,9.5))
for i, low_label, ax in zip(low_index,low_labels, axs.flat):
image = X_validation[i].astype('float32' ).reshape(28, 28)
 ax.imshow(image, cmap='gray')
 ax.set_xlabel("True: {}".format(y_validation.iloc[i]))
ax.set_title("Guessed:{} ".format(low_label))
!pip install efficientnet-pytorch==0.7.1<load_pretrained>
train_image_generator = ImageDataGenerator(
 rescale=1./255,
rotation_range=25,
width_shift_range=.1,
height_shift_range=.1,
zoom_range=0.1 
)
 validation_image_generator = ImageDataGenerator(
 rescale=1./255,
rotation_range=25,
width_shift_range=.1,
height_shift_range=.1,
zoom_range=0.1 
)
 train_image_gen = train_image_generator.flow(
 x=X_train,
y=y_train, 
)
 validation_image_gen = validation_image_generator.flow(
 x=X_validation,
y=y_validation, 
)
 lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(
 initial_learning_rate=0.001,
decay_steps=250,
decay_rate=1,
staircase=False 
)
 def get_optimizer() :
return tf.keras.optimizers.Adam(lr_schedule)
 tf.keras.backend.clear_session()
model.compile(optimizer=get_optimizer() ,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(
 train_image_gen,
epochs=20,
validation_data=validation_image_gen
)
model = EfficientNet.from_pretrained('efficientnet-b7', num_classes=4)
 model = model.to(device )<import_modules>
X_test = test.astype('float16' ).values
X_test = X_test / 255.0
X_test = X_test.reshape(len(X_test), 28, 28, 1 )
<choose_model_class><EOS> 
label_pred = model.predict_classes(X_test, verbose=0)
 submission = pd.DataFrame()
submission['Label'] = label_pred
submission['ImageId'] = submission.index + 1
submission.to_csv('.. /working/output.csv', index=False )