Kaggle competition: Digit Recognizer
input_size = 384
stats =([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261])
 trans1 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.Pad(8, padding_mode='reflect'),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans2 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomHorizontalFlip(p=0.3),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans3 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomVerticalFlip(p=0.3),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans4 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 transs = [trans1, trans2, trans3, trans4]<prepare_x_and_y>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
test_path = '/kaggle/input/cassava-leaf-disease-classification/test_images/'
test_images = os.listdir(test_path)
 train_image_paths = [os.path.join(test_path, x)for x in test_images]
y_preds = []
y2_preds = []
p = 0
for i in test_images:
res = []
image = Image.open(f'/kaggle/input/cassava-leaf-disease-classification/test_images/{i}')
 input_size = 384
outs = torch.Tensor(np.zeros(( len(transs), 5)))
 outs2 = torch.Tensor(np.zeros(( len(transs), 5)))
 k = 0
for trans in transs:
img = trans(image)
 img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
 img = Variable(img.to(device))
out = resnet(img)
 out2= xception(img)
 outs[k,:] = 4*out
outs2[k,:] = 5*out2
k += 1
out = outs.mean(axis=0)
 out2 = outs2.mean(axis=0)
 res.append(out2)
 mean = torch.mean(torch.stack(res), dim = 0)
 _, predicted = torch.max(out2.data, 0)
 y_preds.append(predicted.item() )<create_dataframe>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
df_sub = pd.DataFrame({'image_id': test_images, 'label': y_preds})
 display(df_sub )<save_to_csv>
X_train = X_train / 255.0
test = test / 255.0
df_sub.to_csv('submission.csv', index=False )<install_modules>
Y_train=to_categorical(Y_train, num_classes=10 )
pip install cleantext<install_modules>
random_seed=2
pip install ktrain<set_options>
X_train,X_val,Y_train,Y_val = train_test_split(X_train,Y_train,test_size=0.1,random_state=random_seed )
warnings.filterwarnings("ignore" )<load_from_csv>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
df = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 display(df.head())
 display(df.shape )<categorify>
optimizer = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0 )
l=len(df)
 display(l)
 cleanlist=[]
textlength=[]
for i in range(l):
ct=cleantext.clean(df.iloc[i,3], clean_all= True)
 cleanlist.append(ct)
 lct=len(ct)
 textlength.append(lct)
<create_dataframe>
model.compile(optimizer=optimizer,loss="categorical_crossentropy",metrics=["accuracy"] )
df_clean=pd.DataFrame(cleanlist)
 df_clean.columns=['cleantext']
frames=[df,df_clean]
newdf=pd.concat(frames, axis=1)
 display(newdf )<train_model>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',patience=3,verbose=1,factor=0.5,min_lr=0.00001 )
( x_train, y_train),(x_test, y_test), preproc=text.texts_from_df(newdf, 'cleantext',label_columns=['target'],
maxlen=127,max_features=100000,
preprocess_mode='bert', val_pct=.1 )<train_model>
epochs=15
batch_size=86
model=text.text_classifier('bert',(x_train, y_train), preproc=preproc)
 learner=ktrain.get_learner(model, train_data=(x_train, y_train),
val_data=(x_test, y_test),
batch_size=32 )<train_model>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
learner.fit_onecycle(2e-5, 3)
 predictor=ktrain.get_predictor(learner.model, preproc )<predict_on_test>
history = model.fit(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
predictor.predict(['calm','earthquake'] )<load_from_csv>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<predict_on_test><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )