Kaggle competition: Humpback Whale Identification Challenge
test_whole = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv',index_col='Date',parse_dates=True)
 test_whole = test_whole.fillna(value='empty' )<feature_engineering>
train_images = glob(".. /input/train/*jpg")
 test_images = glob(".. /input/test/*jpg")
 df = pd.read_csv(".. /input/train.csv")
 df["Image"] = df["Image"].map(lambda x : ".. /input/train/"+x)
 ImageToLabelDict = dict(zip(df["Image"], df["Id"]))
test_whole['state_with_country'] = test_whole['Province_State'] +'_'+ test_whole['Country_Region']
test_whole.head()<define_variables>
SIZE = 64
def ImportImage(filename):
img = Image.open(filename ).convert("LA" ).resize(( SIZE,SIZE))
return np.array(img)[:,:,0]
train_img = np.array([ImportImage(img)for img in train_images])
 x = train_img
state_i_data_for_test=[]
for i in state_with_country_name_for_test:
state_i_data_for_test.append(i)
<feature_engineering>
print("%d training images" %x.shape[0])
 print("Nbr of samples/class\tNbr of classes")
 for index, val in df["Id"].value_counts().value_counts().sort_index().iteritems() :
print("%d\t\t\t%d" %(index,val))
for i,j in enumerate(state_with_country_name_for_test):
state_i_data_for_test[i] = test_whole[test_whole['state_with_country'] == j ]
state_i_data_for_test[i] = state_i_data_for_test[i].iloc[21:]
state_i_data_for_test[i] = state_i_data_for_test[i].drop(['ForecastId','state_with_country','Province_State','Country_Region'],axis=1)
 state_i_data_for_test.append(state_i_data_for_test[i] )<import_modules>
class LabelOneHotEncoder() :
def __init__(self):
self.ohe = OneHotEncoder()
self.le = LabelEncoder()
def fit_transform(self, x):
features = self.le.fit_transform(x)
 return self.ohe.fit_transform(features.reshape(-1,1))
def transform(self, x):
return self.ohe.transform(self.la.transform(x.reshape(-1,1)))
 def inverse_tranform(self, x):
return self.le.inverse_transform(self.ohe.inverse_tranform(x))
def inverse_labels(self, x):
return self.le.inverse_transform(x)
 y = list(map(ImageToLabelDict.get, train_images))
lohe = LabelOneHotEncoder()
y_cat = lohe.fit_transform(y )
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator<normalization>
WeightFunction = lambda x : 1./x**0.75
ClassLabel2Index = lambda x : lohe.le.inverse_tranform([[x]])
 CountDict = dict(df["Id"].value_counts())
 class_weight_dic = { lohe.le.transform([image_name])[0] : WeightFunction(count)for image_name, count in CountDict.items() }
del CountDict
result = []
full_scaler = MinMaxScaler()<train_on_grid>
batch_size = 128
num_classes = len(y_cat.toarray() [0])
 epochs = 9
print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 model = Sequential()
model.add(Conv2D(48, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(48,(3, 3), activation='sigmoid'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(48,(5, 5), activation='sigmoid'))
model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(0.33))
model.add(Flatten())
 model.add(Dense(36, activation='sigmoid'))
model.add(Dropout(0.33))
model.add(Dense(36, activation='sigmoid'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta() ,
metrics=['accuracy'])
 model.summary()
model.fit_generator(image_gen.flow(x_train, y_train.toarray() , batch_size=batch_size),
steps_per_epoch= x_train.shape[0]//batch_size,
epochs=epochs,
verbose=1,
class_weight=class_weight_dic)
 Training accuracy: {1:.4f}'.format(*score))
<prepare_output><EOS> 
with open("sample_submission.csv","w")as f:
with warnings.catch_warnings() :
f.write("Image,Id
")
 warnings.filterwarnings("ignore",category=DeprecationWarning)
 for image in test_images:
img = ImportImage(image)
 x = img.astype("float32")
 x = image_gen.standardize(x.reshape(1,SIZE,SIZE))
y = model.predict_proba(x.reshape(1,SIZE,SIZE,1))
predicted_args = np.argsort(y)[0][::-1][:5]
predicted_tags = lohe.inverse_labels(predicted_args)
 image = split(image)[-1]
predicted_tags = " ".join(predicted_tags)
 f.write("%s,%s
" %(image, predicted_tags))