Kaggle competition: Bag of Words Meets Bags of Popcorn
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<choose_model_class>
train = pd.read_csv('.. /input/labeledTrainData.tsv', delimiter='\t')
 test = pd.read_csv('.. /input/testData.tsv', delimiter='\t')
 train.shape, test.shape
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
class EnsembleClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model1 = VisionTransformer.from_name('ViT-B_16', num_classes=5)
 self.model1.load_state_dict(torch.load('.. /input/vit-model-1/ViT-B_16.pt'))
self.model2 = CassvaImgClassifier(model_arch, n_class, pretrained)
 def forward(self, x):
x1 = self.model1(x)
 x2 = self.model2(x)
 return 0.6 * x1 + 0.4 * x2
def load(self, state_dict):
self.model2.load_state_dict(state_dict )<split>
print("number of rows for sentiment 1: {}".format(len(train[train.sentiment == 1])))
 print("number of rows for sentiment 0: {}".format(len(train[train.sentiment == 0])) )
if __name__ == '__main__':
seed_everything(CFG['seed'])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_inference_transforms() , output_label=False)
 test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = EnsembleClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load(torch.load('.. /input/fork-pytorch-efficientnet-baseline-train-amp-a/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
val_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, val_loader, device)]
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
val_preds = np.mean(val_preds, axis=0)
 tst_preds = np.mean(tst_preds, axis=0)
 print('fold {} validation loss = {:.5f}'.format(fold, log_loss(valid_.label.values, val_preds)))
 print('fold {} validation accuracy = {:.5f}'.format(fold,(valid_.label.values==np.argmax(val_preds, axis=1)).mean()))
del model
torch.cuda.empty_cache()<feature_engineering>
train['length'] = train['review'].apply(len)
 train.head()
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<save_to_csv>
def clean_text(raw_text):
raw_text = BeautifulSoup(raw_text, 'lxml' ).get_text()
letters_only = re.sub("[^a-zA-Z]", " ", raw_text)
 words = letters_only.lower().split()
stops = set(stopwords.words("english"))
return [w for w in words if not w in stops]
test.to_csv('submission.csv', index=False )<import_modules>
train['clean_review'] = train['review'].apply(clean_text)
 train['length_clean_review'] = train['clean_review'].apply(len)
 train.head()
print("tensorflow version" + tf.__version__)
<define_variables>
print(train[train['length_clean_review'] == 4]['review'].iloc[0])
 print('------After Cleaning------')
 print(train[train['length_clean_review'] == 4]['clean_review'].iloc[0] )
BASE_DIR = ".. /input/cassava-leaf-disease-classification"<load_pretrained>
from sklearn.feature_extraction.text import CountVectorizer
with open(os.path.join(BASE_DIR, "label_num_to_disease_map.json")) as file:
map_classes = json.loads(file.read())
 map_classes = {int(k): v for k, v in map_classes.items() }
print(json.dumps(map_classes, indent=4))<define_variables>
bow_transform = CountVectorizer(analyzer=clean_text ).fit(train['review'])
 print(len(bow_transform.vocabulary_))
input_files = os.listdir(os.path.join(BASE_DIR, "train_images"))
print(f"Number of train images: {len(input_files)}" )<feature_engineering>
review_bow = bow_transform.transform(train['review'] )
img_shapes = {}
for image_name in os.listdir(os.path.join(BASE_DIR, "train_images")) [:300]:
image = cv2.imread(os.path.join(BASE_DIR, "train_images", image_name))
img_shapes[image.shape] = img_shapes.get(image.shape, 0)+ 1
print(img_shapes )<load_from_csv>
sparsity =(100.0 * review_bow.nnz /(review_bow.shape[0] * review_bow.shape[1]))
print('sparsity: {}'.format(sparsity))
df_train = pd.read_csv(os.path.join(BASE_DIR , "train.csv"))
df_train["class_name"] = df_train["label"].map(map_classes)
 df_train[:10]<define_variables>
tfidf_transformer = TfidfTransformer().fit(review_bow)
 tfidf1 = tfidf_transformer.transform(bow1)
 print(tfidf1 )
BATCH_SIZE = 8
STEPS_PER_EPOCH = len(df_train)*0.8 / BATCH_SIZE
VALIDATION_STEPS = len(df_train)*0.2 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 512<data_type_conversions>
review_tfidf = tfidf_transformer.transform(review_bow)
 print(review_tfidf.shape )
df_train.label = df_train.label.astype('str')
 train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(validation_split = 0.2,
rescale = 1./255,
rotation_range = 45,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1)
 train_generator = train_datagen.flow_from_dataframe(df_train,
directory = os.path.join(BASE_DIR, "train_images"),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator(validation_split = 0.2)
 validation_generator = validation_datagen.flow_from_dataframe(df_train,
directory = os.path.join(BASE_DIR, "train_images"),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(train['review'], train['sentiment'], test_size=0.22, random_state=101)
 len(X_train), len(X_test), len(X_train)+ len(X_test )
def create_model() :
conv_base = EfficientNetB0(include_top = False, weights = None,
input_shape =(TARGET_SIZE, TARGET_SIZE, 3))
model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model<load_pretrained>
def pred(predicted,compare):
cm = pd.crosstab(compare,predicted)
 TN = cm.iloc[0,0]
FN = cm.iloc[1,0]
TP = cm.iloc[1,1]
FP = cm.iloc[0,1]
print("CONFUSION MATRIX ------->> ")
 print(cm)
 print()
print('Classification paradox :------->>')
 print('Accuracy :- ', round(((TP+TN)*100)/(TP+TN+FP+FN),2))
print()
print('False Negative Rate :- ',round(( FN*100)/(FN+TP),2))
print()
print('False Postive Rate :- ',round(( FP*100)/(FP+TN),2))
print()
print(classification_report(compare,predicted))
model.load_weights('.. /input/weight1/myweights3.h5' )<train_model>
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', LogisticRegression(random_state=101)) ,
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )
early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1)
 history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
callbacks = [early_stop, reduce_lr] 
)
 model.save('./myEffNetB0_512_8__3.h5')
 model.save_weights('./myweights4.h5' )<load_from_csv>
predictions = pipeline.predict(X_test)
 pred(predictions,y_test )
ss = pd.read_csv(os.path.join(BASE_DIR, "sample_submission.csv"))
ss<predict_on_test>
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', MultinomialNB()),
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )
preds = []
for image_id in ss.image_id:
image = Image.open(os.path.join(BASE_DIR, "test_images", image_id))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 ss['label'] = preds
ss<save_to_csv>
predictions = pipeline.predict(X_test)
 pred(predictions,y_test )
ss.to_csv('submission.csv', index = False )<load_from_csv>
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', RandomForestClassifier(n_estimators = 500)) ,
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 images_train, images_val = train_test_split(train, test_size=0.3)
 label_train = images_train['label']
label_val = images_val['label']
images_train = images_train.drop(['label'],axis = 1)
 images_val = images_val.drop(['label'],axis = 1)
 label_train = pd.DataFrame(label_train)
 label_val = pd.DataFrame(label_val)
 enc = OneHotEncoder()
enc.fit(label_train)
 enc.fit(label_val)
 label_train = enc.transform(label_train ).toarray()
label_val = enc.transform(label_val ).toarray()
images_train =(images_train.values ).astype('float32')
 images_val =(images_val.values ).astype('float32')
 images_train = images_train.reshape(images_train.shape[0], 28, 28, 1)
 images_val = images_val.reshape(images_val.shape[0], 28, 28, 1 )<choose_model_class>
predictions = pipeline.predict(X_test)
 pred(predictions,y_test )
model2 = models.Sequential()
model2.add(layers.Conv2D(filters = 128, kernel_size=(5, 5), activation='relu', padding='same', input_shape =(28, 28, 1)))
 model2.add(layers.BatchNormalization())
 model2.add(layers.Conv2D(filters = 64, kernel_size=(5, 5), activation='relu', padding='same', input_shape =(28, 28, 1)))
 model2.add(layers.BatchNormalization())
 model2.add(layers.MaxPooling2D(pool_size=(2, 2)))
 model2.add(layers.Dropout(0.25))
model2.add(layers.Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model2.add(layers.BatchNormalization())
 model2.add(layers.Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model2.add(layers.BatchNormalization())
 model2.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
 model2.add(layers.Dropout(0.25))
model2.add(layers.Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model2.add(layers.BatchNormalization())
 model2.add(layers.Dropout(0.25))
model2.add(layers.Flatten())
 model2.add(layers.Dense(256, activation='relu'))
model2.add(layers.BatchNormalization())
 model2.add(layers.Dropout(0.25))
model2.add(layers.Dense(10, activation='softmax'))
model2.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics='accuracy')
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(images_train)
 estimator = model2.fit(datagen.flow(images_train,label_train, batch_size = 128),
epochs = 50, validation_data =(images_val, label_val),
steps_per_epoch=230
,callbacks=[learning_rate_reduction])
<save_to_csv>
pipeline_logit = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', LogisticRegression(random_state=101)) ,
])
 pipeline_logit.fit(train['review'],train['sentiment'])
 test['sentiment'] = pipeline_logit.predict(test['review'] )
<import_modules><EOS> 
output.to_csv("output.csv", index=False, quoting=3 )