Kaggle competition: Digit Recognizer
test.to_csv('submission.csv', index=False )<define_variables>
input_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )
CONFIG_NAME = 'stacking12.yml'
debug = False
STAGE2_DIR = '.. /input/train-stacking-2dcnn-ver3/output'<define_variables>
input_data = input_df.drop(['label'], axis=1 ).values / 255.0
input_labels = input_df['label']
test_data = test_df.values / 255.0
train_data, valid_data, train_labels, valid_labels = train_test_split(input_data, input_labels, test_size = 0.15, random_state=2)
 train_data = train_data.reshape(-1,28,28,1)
 valid_data = valid_data.reshape(-1,28,28,1)
 test_data = test_data.reshape(-1,28,28,1)
 train_labels = to_categorical(train_labels, 10)
 valid_labels = to_categorical(valid_labels, 10)
 print("Train: ",train_data.shape, train_labels.shape)
 print("Valid: ",valid_data.shape, valid_labels.shape)
 print("Test: ",test_data.shape )
CONFIG_PATH = f'{STAGE2_DIR}/{CONFIG_NAME}'
with open(CONFIG_PATH)as f:
config = yaml.load(f)
 INFO = config['info']
TAG = config['tag']
CFG = config['cfg']
OUTPUT_DIR = './'
DATA_PATH = '.. /input/cassava-leaf-disease-classification'<define_variables>
data_augment = ImageDataGenerator(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1 )
<import_modules>
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32,(5,5), padding='same', activation='relu', input_shape=(28,28,1)) ,
tf.keras.layers.Conv2D(32,(5,5), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3,3), padding='same', activation='relu'),
tf.keras.layers.Conv2D(64,(3,3), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.summary()
sys.path.append('.. /input/pytorch-image-models/pytorch-image-models-master')
 Compose, OneOf, Normalize, Resize, RandomResizedCrop, RandomCrop, HorizontalFlip, VerticalFlip,
RandomBrightness, RandomContrast, RandomBrightnessContrast, Rotate, ShiftScaleRotate, Cutout,
IAAAdditiveGaussianNoise, Transpose, CenterCrop 
)
 warnings.filterwarnings('ignore')
 if CFG['debug']:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 else:
device = torch.device('cuda')
 start_time = datetime.datetime.now()
start_time_str = start_time.strftime('%m%d%H%M' )<load_from_csv>
annealer = ReduceLROnPlateau(
 monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
train = pd.read_csv(f'{DATA_PATH}/train.csv')
 test = pd.read_csv(f'{DATA_PATH}/sample_submission.csv')
 label_map = pd.read_json(f'{DATA_PATH}/label_num_to_disease_map.json',
orient='index')
 if CFG['debug']:
train = train.sample(n=1000, random_state=CFG['seed'] ).reset_index(drop=True )<define_variables>
optim = tf.keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer=optim, loss="categorical_crossentropy", metrics=['accuracy'])
 num_epochs = 30
batch_size = 86
history = model.fit_generator(
 data_augment.flow(train_data, train_labels, batch_size=batch_size),
steps_per_epoch=train_data.shape[0]//batch_size,
validation_data=(valid_data, valid_labels),
epochs=num_epochs,
callbacks=[annealer]
)
model_dirs = []
for stage1 in CFG['stage1_models']:
num = str(stage1 ).rjust(2, '0')
 output_dir_ = glob.glob(f'.. /input/{num}*/')
 assert len(output_dir_)== 1, output_dir_
model_dirs.append(output_dir_[0])
 model_dirs<load_pretrained>
predictions = model.predict(test_data)
 pred_list = []
for index, pred in enumerate(predictions):
pred_list.append({"ImageId": index+1, "Label": np.argmax(pred)})
 sub_df = pd.DataFrame(pred_list)
 sub_df.to_csv("submission.csv", index=False )