Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<normalization>
!pip install -q efficientnet
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )<train_model>
%matplotlib inline
warnings.filterwarnings('ignore' )
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )<train_model>
def seed_everything(seed=13):
np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ['TF_KERAS'] = '1'
random.seed(seed)
 seed_everything(42 )
%%time
epoch_train = 15
mlp = MLP(layer_size=[x_train.shape[1], 28, 28, 28, 1], regularization=1, output_shrink=0.1, output_range=[-1,1], loss_type="hardmse")
 mlp.train(x_train, y, verbose=0, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1)
 del x_train, y
gc.collect()<normalization>
def TPU() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 return strategy
strategy = TPU()
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )<predict_on_test>
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
AUTO = tf.data.experimental.AUTOTUNE
%%time
pred = mlp.predict(x_test)
 del x_test
gc.collect()<prepare_output>
IMG_SIZE_h = 512
IMG_SIZE_w = 512
FOLDS = 5
SEED = 42
EPOCHS = 50
BATCH_SIZE = 8*strategy.num_replicas_in_sync
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2<load_from_csv>
path='.. /input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(path+'train.csv')
 train_id = train['image_id']
train.pop('image_id')
 y_train = train.to_numpy().astype('float32')
 category_names = ['healthy','multiple_diseases','rust','scab']
root = 'images'
images_paths = [(os.path.join(GCS_DS_PATH,root,idee+'.jpg')) for idee in train_id]
df_test = pd.read_csv(INPUT_DIR + 'test_V2.csv' )<feature_engineering>
DIR_INPUT = '/kaggle/input/plant-pathology-2020-fgvc7'
train_df = pd.read_csv(DIR_INPUT + '/train.csv')
 test_df = pd.read_csv(DIR_INPUT + '/test.csv')
 cols = list(train_df.columns[1:])
 train_paths = train_df['image_id'].apply(lambda x: os.path.join(GCS_DS_PATH , 'images' , x + '.jpg')).values
test_paths = test_df['image_id'].apply(lambda x: os.path.join(GCS_DS_PATH , 'images' , x + '.jpg')).values
train_labels = train_df.iloc[:,1:].values
%%time
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc<feature_engineering>
train_df.iloc[:,1:-1].sum(axis=1 ).value_counts()
df_test['winPlacePerc'] = pred<save_to_csv>
def calculate_hash(im):
md5 = hashlib.md5()
md5.update(np.array(im ).tostring())
 return md5.hexdigest()
def get_image_meta(image_id, image_src, dataset='train'):
im = Image.open(image_src)
 extrema = im.getextrema()
meta = {
'image_id': image_id,
'dataset': dataset,
'hash': calculate_hash(im),
'r_min': extrema[0][0],
'r_max': extrema[0][1],
'g_min': extrema[1][0],
'g_max': extrema[1][1],
'b_min': extrema[2][0],
'b_max': extrema[2][1],
'height': im.size[0],
'width': im.size[1],
'format': im.format,
'mode': im.mode
}
return meta
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<set_options>
data = []
for i, image_id in enumerate(tqdm(train_df['image_id'].values, total=train_df.shape[0])) :
data.append(get_image_meta(image_id, DIR_INPUT + '/images/{}.jpg'.format(image_id)) )
%pylab inline 
<categorify>
for i, image_id in enumerate(tqdm(test_df['image_id'].values, total=test_df.shape[0])) :
data.append(get_image_meta(image_id, DIR_INPUT + '/images/{}.jpg'.format(image_id), 'test'))
data = pd.DataFrame.from_csv('.. /input/train.csv')
 y = data['species']
y = LabelEncoder().fit(y ).transform(y)
 y_cat = to_categorical(y)
 margin = data.columns[1:65]
margin = data[margin].as_matrix()
margin = StandardScaler().fit(margin ).transform(margin)
 shape = data.columns[65:129]
shape = data[shape].as_matrix()
shape = StandardScaler().fit(shape ).transform(shape)
 texture = data.columns[129:193]
texture = data[texture].as_matrix()
texture = StandardScaler().fit(texture ).transform(texture)
<choose_model_class>
meta_df = pd.DataFrame(data)
 meta_df.head()
modelMargin = Sequential()
modelMargin.add(Dense(128, input_dim=64, activation='relu'))
modelMargin.add(Dropout(0.7))
modelShape = Sequential()
modelShape.add(Dense(128, input_dim=64, activation='relu'))
modelShape.add(Dropout(0.7))
modelTexture = Sequential()
modelTexture.add(Dense(128, input_dim=64, activation='relu'))
modelTexture.add(Dropout(0.7))
merged = Merge([modelMargin, modelShape, modelTexture], mode='concat' )<train_model>
meta_df.groupby(by='dataset')[['width', 'height']].aggregate(['min', 'max'] )
model = Sequential()
model.add(merged)
 model.add(Dense(99, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
 history = model.fit(
 [margin, shape, texture],
y_cat,
nb_epoch=350,
batch_size=32,
validation_split=0.1,
verbose=0
)<load_from_csv>
duplicates = meta_df.groupby(by='hash')[['image_id']].count().reset_index()
duplicates = duplicates[duplicates['image_id'] > 1]
duplicates.reset_index(drop=True, inplace=True)
 duplicates = duplicates.merge(meta_df[['image_id', 'hash']], on='hash')
 duplicates.head(20 )
test1 = pd.read_csv('.. /input/test.csv')
 test = pd.DataFrame.from_csv('.. /input/test.csv')
 index = test1.pop('id')
 testMargin = test[test.columns[0:64]].as_matrix()
testShape = test[test.columns[64:128]].as_matrix()
testTexture = test[test.columns[128:192]].as_matrix()
testMargin = StandardScaler().fit(testMargin ).transform(testMargin)
 testShape = StandardScaler().fit(testShape ).transform(testShape)
 testTexture = StandardScaler().fit(testTexture ).transform(testTexture)
 yPred = model.predict_proba(
 [testMargin, testShape, testTexture] 
)
 columns = data['species'].unique()
yPred = pd.DataFrame(yPred, index=index, columns=sort(columns))
fp = open('merged_nn2.csv','w')
 fp.write(yPred.to_csv() )<set_options>
def prepare_train(train_paths, train_labels):
data =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 return data
def prepare_val(val_paths, val_labels):
data =(
 tf.data.Dataset 
.from_tensor_slices(( val_paths, val_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 return data
def prepare_test(test_paths):
data =(
 tf.data.Dataset 
.from_tensor_slices(( test_paths)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
)
 return data
%matplotlib inline
target_size =(256, 256)
 grayscale = True
train_path = '.. /input/train.csv'
test_path = '.. /input/test.csv'
submission_path = '.. /input/sample_submission.csv'
submission_output = './submission.csv'
def load_image(id):
img_path = '.. /input/images/%d.jpg' %(id,)
 img = image.load_img(img_path,
grayscale=grayscale)
 img.thumbnail(target_size)
 bg = Image.new('L', target_size,(0,))
bg.paste(
 img,(int(( target_size[0] - img.size[0])/ 2), int(( target_size[1] - img.size[1])/ 2)) 
)
 img_arr = image.img_to_array(bg)
 return img_arr<categorify>
def decode_image(filename, label=None, image_size=(IMG_SIZE_h, IMG_SIZE_w)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=5050):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 image = tf.image.random_crop(image,size=[IMG_SIZE_h,IMG_SIZE_w,3],seed=seed)
 image = tf.image.random_brightness(image,max_delta=0.5, seed=seed)
 if label is None:
return image
else:
return image, label
train_data = pd.read_csv(train_path)
 x_ids = train_data.iloc[:, 0]
x_images = list()
for i in x_ids:
x_images.append(load_image(i))
x_images = np.array(x_images)
 plt.imshow(x_images[0].squeeze())
 plt.show()
print('Shape of images', x_images[0].shape)
 x_features = train_data.iloc[:, 2:].values
print('Number of features', x_features.shape[1])
 y = train_data['species']
le = LabelEncoder()
le.fit(y)
 y = le.transform(y)
 nb_classes = len(le.classes_)
 print('Number of classes', nb_classes)
 print('Number of instances', len(y))
plt.hist(y, bins=nb_classes)
 plt.title('Number of instances in each class')
 plt.xlabel('Class id')
 plt.ylabel('Number of instances')
 plt.show()
y = np_utils.to_categorical(y)
 test_data = pd.read_csv(test_path)
 test_ids = test_data.iloc[:, 0]
test_images = list()
for i in test_ids:
test_images.append(load_image(i))
test_images = np.array(test_images)
 submission_data = pd.read_csv(submission_path )<train_model>
def outer_product(x):
phi_I = tf.einsum('ijkm,ijkn->imn',x[0],x[1])
 phi_I = tf.reshape(phi_I,[-1,x[0].shape[3]*x[1].shape[3]])
 size1 = int(x[1].shape[1])
 size2 = int(x[1].shape[2])
 phi_I = tf.divide(phi_I, size1*size2)
 y_ssqrt = tf.multiply(tf.sign(phi_I),tf.sqrt(tf.abs(phi_I)+1e-12))
z_l2 = tf.nn.l2_normalize(y_ssqrt, axis=1)
 return z_l2
sss = StratifiedShuffleSplit(10, 0.2, random_state=15)
 for train_index, test_index in sss.split(x_images, y):
	x_train_images, x_test_images, x_train_features, x_test_features = x_images[train_index], x_images[test_index], x_features[train_index], x_features[test_index]
	y_train, y_test = y[train_index], y[test_index]
print('Shape of x train images', x_train_images.shape)
 print('Shape of x train features', x_train_features.shape)
 print('Shape of y train', y_train.shape)
 print('Shape of x test images', x_test_images.shape)
 print('Shape of x test features', x_test_features.shape)
 print('Shape of y test', y_test.shape )<choose_model_class>
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
def construct_feature_model() :
print('Contructing the model')
 model1 = Sequential([
Dense(nb_classes * 2, input_shape=x_train_features.shape[1:]),
BatchNormalization() ,
Activation('tanh'),
Dropout(0.25)
 ])
 model2 = Sequential([
Convolution2D(32, 3, 3, border_mode='same',
input_shape=x_images.shape[1:]),
Activation('tanh'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) ,
Dropout(0.5),
Flatten() ,
Dense(nb_classes),
Activation('tanh')
 ])
 model = Sequential([
Merge([model1, model2], mode='concat', concat_axis=1),
Dense(nb_classes * 2),
Activation('tanh'),
Dropout(0.5),
Dense(nb_classes),
Activation('softmax')
 ])
 optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer=optimizer,
loss='categorical_crossentropy',
metrics=['accuracy'])
 print('Finish construction of the model')
 return model
model = construct_feature_model()<train_model>
def get_model() :
input_tensor = Input(shape=(IMG_SIZE_h,IMG_SIZE_w,3))
model1 = efn.EfficientNetB3(weights='imagenet', include_top=False, input_tensor=input_tensor,input_shape=(IMG_SIZE_h, IMG_SIZE_w, 3))
model2 = efn.EfficientNetB3(weights='noisy-student', include_top=False, input_tensor=input_tensor,input_shape=(IMG_SIZE_h, IMG_SIZE_w, 3))
for i, layer in enumerate(model1.layers):
layer._name = 'model1_' + layer.name
last_layer1 = model1.get_layer('model1_top_conv')
 last_output1 = last_layer1.output
for i, layer in enumerate(model2.layers):
layer._name = 'model2_' + layer.name
last_layer2 = model2.get_layer('model2_top_conv')
 last_output2 = last_layer2.output
model1_ = Model(inputs=model1.input, outputs=last_output1)
 model2_ = Model(inputs=model2.input, outputs=last_output2)
 model1_.compile(Adam(lr=0.0003, decay=1e-3),loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0))
model2_.compile(Adam(lr=0.0003, decay=1e-3),loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0))
d1=model1_.output
d2=model2_.output
bilinear = Lambda(outer_product, name='outer_product1' )([d1,d2])
 predictions=Dense(4, activation='softmax', name='predictions' )(bilinear)
 model = Model(inputs=model1.input, outputs=predictions)
 opt = Adam(lr=0.0003, decay=1e-3)
 model.compile(optimizer=opt, loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0),metrics=[f1,'categorical_accuracy'])
 return model
print('Start to fit')
 s = time.time()
best_model_file = 'leaf.h5'
best_model_cb = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=0, save_best_only=True)
 batch_size = 32
nb_epoch = 8
verbose = 2
callbacks = [best_model_cb]
validation_split = 0.0
validation_data =([x_test_features, x_test_images], y_test)
 shuffle = True
class_weight = None
sample_weight = None
data_augmentation = False
if not data_augmentation:
print('Not using data augmentation')
 history = model.fit([x_features, x_images], y,
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=verbose,
callbacks=callbacks,
validation_split=validation_split,
validation_data=validation_data,
shuffle=shuffle,
class_weight=class_weight,
sample_weight=sample_weight)
 print('Finish fitting
Fitting time', time.time() - s )<save_to_csv>
def Callbacks() :
erl = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='min',
restore_best_weights=True)
 rdc = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=2, verbose=1, mode='min')
 return [erl,rdc]
model = load_model(best_model_file)
 y_prob = model.predict([test_data.iloc[:, 1:].values, test_images])
 submission_data.iloc[:, 1:] = y_prob
submission_data.tail()
f = open(submission_output, 'w')
 f.write(pd.DataFrame(submission_data ).to_csv(index = False))
f.close()<create_dataframe>
%%time
skf = StratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=SEED)
 test_pred = []
val_roc_auc = []
for i,(train_idx, val_idx)in enumerate(skf.split(train_paths, train_labels.argmax(1))):
print() ; print('
print('
print('
X_train, X_val = train_paths[train_idx], train_paths[val_idx]
y_train, y_val = train_labels[train_idx], train_labels[val_idx]
strategy = TPU()
with strategy.scope() :
model = get_model()
history = model.fit(
 prepare_train(X_train,y_train),
steps_per_epoch=y_train.shape[0] // BATCH_SIZE,
validation_data=prepare_val(X_val, y_val),
validation_steps=y_val.shape[0] // BATCH_SIZE,
callbacks=Callbacks() ,
epochs=EPOCHS,
verbose=1 
)
 test_pred.append(model.predict(prepare_test(test_paths), verbose=1))
val_roc_auc.append(roc_auc_score(y_val,model.predict(prepare_val(X_val, y_val), verbose=1)) )
<define_variables>
model_json = model.to_json()
with open("Model.json", "w")as json_file:
json_file.write(model_json)
 model.save_weights("Model.h5" )
root = '.. /input'
np.random.seed(2016)
 split_random_state = 7
split =.9
def load_numeric_training(standardize=True):

 data = pd.read_csv(os.path.join(root, 'train.csv'))
ID = data.pop('id')
 y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 X = StandardScaler().fit(data ).transform(data)if standardize else data.values
return ID, X, y
def load_numeric_test(standardize=True):

 test = pd.read_csv(os.path.join(root, 'test.csv'))
ID = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)if standardize else test.values
return ID, test
def resize_img(img, max_dim=96):

 max_ax = max(( 0, 1), key=lambda i: img.size[i])
 scale = max_dim / float(img.size[max_ax])
 return img.resize(( int(img.size[0] * scale), int(img.size[1] * scale)))
 def load_image_data(ids, max_dim=96, center=True):

 X = np.empty(( len(ids), max_dim, max_dim, 1))
for i, idee in enumerate(ids):
x = resize_img(load_img(os.path.join(root, 'images', str(idee)+ '.jpg'), grayscale=True), max_dim=max_dim)
 x = img_to_array(x)
 length = x.shape[0]
width = x.shape[1]
if center:
h1 = int(( max_dim - length)/ 2)
 h2 = h1 + length
w1 = int(( max_dim - width)/ 2)
 w2 = w1 + width
else:
h1, w1 = 0, 0
h2, w2 =(length, width)
 X[i, h1:h2, w1:w2, 0:1] = x
return np.around(X / 255.0)
 def load_train_data(split=split, random_state=None):

 ID, X_num_tr, y = load_numeric_training()
X_img_tr = load_image_data(ID)
 sss = StratifiedShuffleSplit(n_splits=1, train_size=split, random_state=random_state)
 train_ind, test_ind = next(sss.split(X_num_tr, y))
X_num_val, X_img_val, y_val = X_num_tr[test_ind], X_img_tr[test_ind], y[test_ind]
X_num_tr, X_img_tr, y_tr = X_num_tr[train_ind], X_img_tr[train_ind], y[train_ind]
return(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)
 def load_test_data() :

 ID, X_num_te = load_numeric_test()
X_img_te = load_image_data(ID)
 return ID, X_num_te, X_img_te
print('Loading the training data...')
(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)= load_train_data(random_state=split_random_state)
 y_tr_cat = to_categorical(y_tr)
 y_val_cat = to_categorical(y_val)
 print('Training data loaded!' )<concatenate>
json_file = open('Model.json', 'r')
 loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
 loaded_model.load_weights("Model.h5")
class ImageDataGenerator2(ImageDataGenerator):
def flow(self, X, y=None, batch_size=32, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg'):
return NumpyArrayIterator2(
 X, y, self,
batch_size=batch_size, shuffle=shuffle, seed=seed,
dim_ordering=self.dim_ordering,
save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format)
 class NumpyArrayIterator2(NumpyArrayIterator):
def next(self):
with self.lock:
self.index_array, current_index, current_batch_size = next(self.index_generator)
 batch_x = np.zeros(tuple([current_batch_size] + list(self.X.shape)[1:]))
for i, j in enumerate(self.index_array):
x = self.X[j]
x = self.image_data_generator.random_transform(x.astype('float32'))
x = self.image_data_generator.standardize(x)
 batch_x[i] = x
if self.save_to_dir:
for i in range(current_batch_size):
img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
 fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
index=current_index + i,
hash=np.random.randint(1e4),
format=self.save_format)
 img.save(os.path.join(self.save_to_dir, fname))
if self.y is None:
return batch_x
batch_y = self.y[self.index_array]
return batch_x, batch_y
print('Creating Data Augmenter...')
 imgen = ImageDataGenerator2(
 rotation_range=20,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 imgen_train = imgen.flow(X_img_tr, y_tr_cat, seed=np.random.randint(1, 10000))
print('Finished making data augmenter...' )<choose_model_class>
all_test = 0
for i in range(FOLDS):
all_test += test_pred[i]
def combined_model() :
image = Input(shape=(96, 96, 1), name='image')
 x = Convolution2D(8, 5, 5, input_shape=(96, 96, 1), border_mode='same' )(image)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x =(Convolution2D(32, 5, 5, border_mode='same'))(x)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x = Flatten()(x)
 numerical = Input(shape=(192,), name='numerical')
 concatenated = merge([x, numerical], mode='concat')
 x = Dense(100, activation='relu' )(concatenated)
 x = Dropout (.5 )(x)
 out = Dense(99, activation='softmax' )(x)
 model = Model(input=[image, numerical], output=out)
 model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
 return model
print('Creating the model...')
 model = combined_model()
print('Model created!' )<prepare_x_and_y>
def combined_generator(imgen, X):

 while True:
for i in range(X.shape[0]):
batch_img, batch_y = next(imgen)
 x = X[imgen.index_array]
yield [batch_img, x], batch_y
best_model_file = "leafnet.h5"
best_model = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=1, save_best_only=True)
 print('Training model...')
 history = model.fit_generator(combined_generator(imgen_train, X_num_tr),
samples_per_epoch=X_num_tr.shape[0],
nb_epoch=89,
validation_data=([X_img_val, X_num_val], y_val_cat),
nb_val_samples=X_num_val.shape[0],
verbose=0,
callbacks=[best_model])
 print('Loading the best model...')
 model = load_model(best_model_file)
 print('Best Model loaded!' )<save_to_csv>
all_models = all_test/FOLDS
all_models
LABELS = sorted(pd.read_csv(os.path.join(root, 'train.csv')).species.unique())
 index, test, X_img_te = load_test_data()
yPred_proba = model.predict([X_img_te, test])
 yPred = pd.DataFrame(yPred_proba,index=index,columns=LABELS)
 print('Creating and writing submission...')
 fp = open('submit.csv', 'w')
 fp.write(yPred.to_csv())
 print('Finished writing submission')
 yPred.tail()<load_from_csv>
sumb = pd.read_csv('.. /input/plant-pathology-2020-fgvc7/sample_submission.csv')
 sumb.iloc[:,1:] = all_models
sumb.head()
submission0 = pd.read_csv('/kaggle/input/m5-final-models/submission_LSTM.csv')
 submission1 = pd.read_csv('/kaggle/input/m5-final-models/submission_XGBoost.csv')
 submission2 = pd.read_csv('/kaggle/input/m5-final-models/submission_LGBM.csv')
 submission3 = pd.read_csv('/kaggle/input/m5-final-models/submission_prophet.csv')
 submission4 = pd.read_csv('/kaggle/input/m5-final-models/submission_SARIMAX.csv' )<load_from_csv>
sumb.to_csv('submission.csv', index=False)
 pd.Series(np.argmax(sumb[cols].values,axis=1)).value_counts()
calendar = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 prices = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
 validation = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 sample_sub = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sample_submission.csv' )<merge>
def preprocess(df,test=False):
paths = df.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
labels = df.loc[:,'healthy':].values
if test==False:
return paths,labels
else:
return paths
def perfect_sub() :
submission = OperateBaseModels(submission0,submission1, a=0, b=1)
 diference = validation.merge(submission, how='right')
 shift= 56
perfect_submission = diference[diference.columns[-shift:-shift+28]]
col = { 'd_'+str(1914+i):'F'+str(i+1)for i in range(28)}
perfect_submission = perfect_submission.rename(columns=col)
 perfect_submission.insert(loc=0, column='id', value= submission0['id'])
 perfect_submission = perfect_submission.fillna(0)
 return perfect_submission
def OperateBaseModels(ModelA,ModelB, a,b):
submissionMerge = pd.merge(ModelA, ModelB, on='id', how='left', suffixes=('_x', '_y'))
submissionMerge
submission = pd.DataFrame()
submission.insert(loc=0, column='id', value= ModelB['id'])
 for j in range(28):
i =j+1
submission.insert(loc=i, column='F'+str(i), value=(( submissionMerge[submissionMerge.columns[i]]*a)+(submissionMerge[submissionMerge.columns[i+28]])*b)/(a+b))
return submission<import_modules>
TTA = 4
test_pred_tta = np.zeros(( len(test_df),4))
for i in range(TTA):
test_dataset_tta =(tf.data.Dataset 
.from_tensor_slices(preprocess(test_df,test=True)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_pred_tta += model.predict(test_dataset_tta, verbose=1)
 sumb = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 sumb[['healthy', 'multiple_diseases', 'rust', 'scab']] = test_pred_tta/TTA
sumb.to_csv('submission_TTA.csv', index=False)
 pd.Series(np.argmax(sumb[cols].values,axis=1)).value_counts()