Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<train_model>
!pip install.. /input/mmcvwhl/addict-2.2.1-py3-none-any.whl
!pip install.. /input/mmdetection20-5-13/mmcv-0.5.1-cp37-cp37m-linux_x86_64.whl
!pip install.. /input/mmdetection20-5-13/terminal-0.4.0-py3-none-any.whl
!pip install.. /input/mmdetection20-5-13/terminaltables-3.1.0-py3-none-any.whl
load_pkl(num_model, num_epochs, conv)
 loss = total_loss(num_model, conv, train_images, train_labels)
 predictions(num_model, conv, loss )<load_from_csv>
cd cocoapi/PythonAPI
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X_train = train_df.iloc[:, 1:].values.astype('float32')
 y_train = train_df.iloc[:, 0].values.astype('int16')
 X_test = test_df.values.astype('float32' )<categorify>
!make install
X_train = X_train / 255
X_test = X_test / 255
y_train = to_categorical(y_train)
 y_train.shape<define_variables>
!python setup.py install
seed = 10
np.random.seed(seed )<import_modules>
import pycocotools
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, BatchNormalization, Dense, Flatten, Dropout<split>
!pip install -v -e .
train_x, val_x, train_y, val_y = train_test_split(X_train, y_train, test_size = 0.10, random_state = 42, stratify = y_train )<choose_model_class>
config_txt =
config_file = open("/kaggle/working/mmdetection/config.py", "w")
 n = config_file.write(config_txt)
 config_file.close()
model = Sequential()
model.add(Convolution2D(32,(5, 5), activation = 'relu', padding = 'same', input_shape =(28, 28, 1)))
 model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Convolution2D(64,(3, 3), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Dropout(0.3))
model.add(Convolution2D(128,(3, 3), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Dropout(0.25))
model.add(Convolution2D(256,(3, 3), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Flatten())
 model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(10, activation = 'softmax'))
model.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'] )<train_model>
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings)
history = model.fit(train_x, train_y, epochs = 15, batch_size = 64, validation_data =(val_x, val_y))<save_to_csv>
checkpoint_path = '.. /input/resnest3fcos1iouatseven/epoch_40.pth'
config_path = '/kaggle/working/mmdetection/config.py'
model = init_detector(config_path, checkpoint_path, device='cuda:0')
 val_df = pd.read_csv('.. /input/global-wheat-detection/sample_submission.csv')
 all_image_ids = set(val_df['image_id'].unique())
 pred_threshold = 0.25
pred_results = []
for image_id in all_image_ids:
img = '.. /input/global-wheat-detection/test/' + image_id + '.jpg'
result = inference_detector(model, img)
 boxes = result[0][:, 0:4]
scores = result[0][:, 4]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= pred_threshold].astype(np.int32)
 scores = scores[scores >= pred_threshold]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
pred_results.append(result)
 test_df = pd.DataFrame(pred_results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False )