Kaggle competition: State Farm Distracted Driver Detection
<SOS> metric: MulticlassLoss Kaggle data source: state-farm-distracted-driver-detection<install_modules>
import tensorflow as tf
!pip install.. /input/my-wheels/scikit_learn-0.21.0-cp37-cp37m-manylinux1_x86_64.whl 
<import_modules>
%matplotlib inline
skopt.__version__, sklearn.__version__<define_variables>
directory = '.. /input/state-farm-distracted-driver-detection/imgs/train'
test_directory = '.. /input/state-farm-distracted-driver-detection/imgs/test/'
random_test = '.. /input/driver/'
classes = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9']
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(seed=42 )<categorify>
img_size1 = 64
img_size2 = 64
def preprocess(df):
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 df.loc[:, 'cp_time'] = df.loc[:, 'cp_time'].map({24: 0, 48: 1, 72: 2})
 del df['sig_id']
return df 
<define_variables>
new_img = cv2.resize(test_array,(img_size2,img_size1))
plt.imshow(new_img,cmap='gray')
 plt.show()
top_feats = [ 0, 1, 2, 3, 5, 6, 8, 9, 10, 11, 12, 14, 15,
16, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 44, 45, 46,
48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 76,
78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92,
93, 94, 95, 96, 97, 99, 100, 101, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
149, 150, 151, 152, 153, 154, 155, 157, 159, 160, 161, 163, 164,
165, 166, 167, 168, 169, 170, 172, 173, 175, 176, 177, 178, 180,
181, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 193, 195,
197, 198, 199, 202, 203, 205, 206, 208, 209, 210, 211, 212, 213,
214, 215, 218, 219, 220, 221, 222, 224, 225, 227, 228, 229, 230,
231, 232, 233, 234, 236, 238, 239, 240, 241, 242, 243, 244, 245,
246, 248, 249, 250, 251, 253, 254, 255, 256, 257, 258, 259, 260,
261, 263, 265, 266, 268, 270, 271, 272, 273, 275, 276, 277, 279,
282, 283, 286, 287, 288, 289, 290, 294, 295, 296, 297, 299, 300,
301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 315,
316, 317, 320, 321, 322, 324, 325, 326, 327, 328, 329, 330, 331,
332, 333, 334, 335, 338, 339, 340, 341, 343, 344, 345, 346, 347,
349, 350, 351, 352, 353, 355, 356, 357, 358, 359, 360, 361, 362,
363, 364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 377,
378, 379, 380, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
392, 393, 394, 395, 397, 398, 399, 400, 401, 403, 405, 406, 407,
408, 410, 411, 412, 413, 414, 415, 417, 418, 419, 420, 421, 422,
423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435,
436, 437, 438, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450,
452, 453, 454, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465,
466, 468, 469, 471, 472, 473, 474, 475, 476, 477, 478, 479, 482,
483, 485, 486, 487, 488, 489, 491, 492, 494, 495, 496, 500, 501,
502, 503, 505, 506, 507, 509, 510, 511, 512, 513, 514, 516, 517,
518, 519, 521, 523, 525, 526, 527, 528, 529, 530, 531, 532, 533,
534, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547,
549, 550, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563,
564, 565, 566, 567, 569, 570, 571, 572, 573, 574, 575, 577, 580,
581, 582, 583, 586, 587, 590, 591, 592, 593, 595, 596, 597, 598,
599, 600, 601, 602, 603, 605, 607, 608, 609, 611, 612, 613, 614,
615, 616, 617, 619, 622, 623, 625, 627, 630, 631, 632, 633, 634,
635, 637, 638, 639, 642, 643, 644, 645, 646, 647, 649, 650, 651,
652, 654, 655, 658, 659, 660, 661, 662, 663, 664, 666, 667, 668,
669, 670, 672, 674, 675, 676, 677, 678, 680, 681, 682, 684, 685,
686, 687, 688, 689, 691, 692, 694, 695, 696, 697, 699, 700, 701,
702, 703, 704, 705, 707, 708, 709, 711, 712, 713, 714, 715, 716,
717, 723, 725, 727, 728, 729, 730, 731, 732, 734, 736, 737, 738,
739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765,
766, 767, 769, 770, 771, 772, 774, 775, 780, 781, 782, 783, 784,
785, 787, 788, 790, 793, 795, 797, 799, 800, 801, 805, 808, 809,
811, 812, 813, 816, 819, 820, 821, 822, 823, 825, 826, 827, 829,
831, 832, 833, 834, 835, 837, 838, 839, 840, 841, 842, 844, 845,
846, 847, 848, 850, 851, 852, 854, 855, 856, 858, 860, 861, 862,
864, 867, 868, 870, 871, 873, 874]
print(len(top_feats))<define_variables>
kernel = np.ones(( 5,5), np.uint8)
 img_dl = cv2.dilate(new_img, kernel, iterations=1)
 plt.imshow(img_dl,cmap='gray')
 plt.show()
PERM_IMP = True
OHE = False
N_TRIALS = 15
SEED = 34<load_from_csv>
def addGaussianNoise(new_img):
row,col= new_img.shape
mean = 0
var = 0.1
sigma = 100
gauss = np.random.normal(mean,sigma,(row,col))
gauss = gauss.reshape(row,col)
 noisy = new_img + gauss
return noisy
gau_noi_img = addGaussianNoise(new_img)
 plt.imshow(gau_noi_img,cmap='gray')
 plt.show()
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 subm = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<categorify>
def add_Salt_Pepper_Noise(new_img, s_vs_p = 0.5, amount = 0.05):
row,col = new_img.shape
s_and_p = np.copy(new_img)
 num_salt = np.ceil(amount * new_img.size * s_vs_p)
 coords = [np.random.randint(0, i - 1, int(num_salt))
for i in new_img.shape]
s_and_p[coords] = 1
num_pepper = np.ceil(amount* new_img.size *(1.- s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in new_img.shape]
s_and_p[coords] = 0
return s_and_p
sap_noi_img = add_Salt_Pepper_Noise(new_img, s_vs_p = 0.5, amount = 0.05)
 plt.imshow(sap_noi_img,cmap='gray')
 plt.show()
cat_features = ['cp_type', 'cp_time', 'cp_dose']
num_features = [c for c in train_features.columns if train_features.dtypes[c] != 'object']
num_features = [c for c in num_features if c not in cat_features]
target_cols = [c for c in train_targets.columns if c not in ['sig_id']]
targets = train_targets[target_cols]
train_x = preprocess(train_features)
 test_x = preprocess(test_features)
 print('Tensor shapes:', train_x.shape, targets.shape, test_x.shape)
 if PERM_IMP:
train_x = train_x.iloc[:, top_feats]
test_x = test_x.iloc[:, top_feats]
if OHE:
train_x = pd.get_dummies(train_x, columns=train_x[cat_features].columns)
 test_x = pd.get_dummies(test_x, columns=test_x[cat_features].columns)
 print('Final tensor shapes:', train_x.shape, targets.shape, test_x.shape )<split>
height,width = new_img.shape[:2]
center =(int(width/2), int(height/2))
angle = 45
M = cv2.getRotationMatrix2D(center, angle, 1)
 rotated_img = cv2.warpAffine(new_img, M,(width, height))
plt.imshow(rotated_img,cmap='gray')
 plt.show()
x_train, x_val, y_train, y_val = train_test_split(train_x, targets, test_size=0.2)
 x_train.shape, x_val.shape<init_hyperparams>
try_img_size1 = 128
try_img_size2 = 128
dim_num_dense_nodes_3 = Integer(low=256, high=512, name='num_dense_nodes_3')
 dim_activation = Categorical(categories=['relu', 'elu'], name='activation')
 dim_dropout1 = Integer(low=1, high=5, name='dp1')
 dim_dropout2 = Integer(low=1, high=5, name='dp2')
 dim_dropout3 = Integer(low=1, high=5, name='dp3')
 dim_dropout4 = Integer(low=1, high=5, name='dp4')
 dim_look_ahead = Integer(low=5, high=15, name='look_ahead')
 dimensions = [
dim_num_dense_nodes_3,
dim_activation,
dim_dropout1,
dim_dropout2,
dim_dropout3,
dim_dropout4,
dim_look_ahead
]
default_params = [512, 'relu', 5, 5, 5, 5, 6]
assert len(default_params)==len(dimensions), 'Error: check shapes!'<choose_model_class>
img = 'img_58997.jpg'
test_array = []
img_array = cv2.imread(os.path.join(test_directory,img),cv2.IMREAD_GRAYSCALE)
 test_array = img_array
plt.imshow(img_array, cmap='gray')
 plt.show()
n_inputs = len(top_feats)
 n_outs = len(target_cols)
 def create_model(
 num_dense_nodes_3,
activation,
dp1,
dp2,
dp3,
dp4,
look_ahead 
):
weight_norm = False
inp = tf.keras.layers.Input(n_inputs)
 x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(0.2 )(x)
 x = tf.keras.layers.Dense(2048, activation=activation )(x)
 if weight_norm:
x = tfa.layers.WeightNormalization(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.1*dp1 )(x)
 x = tf.keras.layers.Dense(1024, activation=activation )(x)
 if weight_norm:
x = tfa.layers.WeightNormalization(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.1*dp2 )(x)
 x = tf.keras.layers.Dense(512, activation=activation )(x)
 if weight_norm:
x = tfa.layers.WeightNormalization(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.1*dp3 )(x)
 x = tf.keras.layers.Dense(num_dense_nodes_3, activation=activation )(x)
 if weight_norm:
x = tfa.layers.WeightNormalization(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.1*dp4 )(x)
 out = tf.keras.layers.Dense(n_outs, activation="sigmoid" )(x)
 model = tf.keras.models.Model(inp, out)
 opt = tf.keras.optimizers.Adam(lr=1e-3)
 opt = tfa.optimizers.Lookahead(opt, sync_period=int(look_ahead))
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['AUC'])
 return model<define_variables>
new_img = cv2.resize(test_array,(try_img_size2,try_img_size1))
plt.imshow(new_img,cmap='gray')
 plt.show()
path_best_model = './model.h5'
best_loss = np.inf<train_model>
training_data = []
i = 0
def create_training_data() :
for category in classes:
path = os.path.join(directory,category)
 class_num = classes.index(category)
 for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
 new_img = cv2.resize(img_array,(img_size2,img_size1))
ret, bin_img = cv2.threshold(new_img, 128, 255, cv2.THRESH_BINARY)
 training_data.append([
bin_img,class_num])
 sap_img = add_Salt_Pepper_Noise(bin_img, s_vs_p = 0.5, amount = 0.01)
 training_data.append([
sap_img,class_num])
 sap_img = add_Salt_Pepper_Noise(bin_img, s_vs_p = 0.5, amount = 0.03)
 training_data.append([
sap_img,class_num])
 sap_img = add_Salt_Pepper_Noise(bin_img, s_vs_p = 0.5, amount = 0.05)
 training_data.append([
sap_img,class_num])
 height,width = new_img.shape[:2]
center =(int(width/2), int(height/2))
angle = 20
M = cv2.getRotationMatrix2D(center, angle, 1)
 rotated_img = cv2.warpAffine(bin_img, M,(width, height))
training_data.append([
rotated_img,class_num])
 moving_x = -10
moving_y = -10
M = np.float32([[1, 0, moving_x], [0, 1, moving_y]])
 shifted_img = cv2.warpAffine(bin_img, M,(width, height))
training_data.append([
shifted_img,class_num] )
@use_named_args(dimensions=dimensions)
 def fitness(num_dense_nodes_3, activation, dp1, dp2, dp3, dp4, look_ahead):

 print('num_dense_nodes layer-4:', num_dense_nodes_3)
 print('activation:',activation)
 print('dropout 1:', dp1*0.1)
 print('dropout 2:', dp2*0.1)
 print('dropout 3:', dp3*0.1)
 print('dropout 4:', dp4*0.1)
 print('look ahead:',look_ahead)
 print()
model = create_model(
 num_dense_nodes_3,
activation,
dp1,
dp2,
dp3,
dp4,
look_ahead)
 cbs = [
tf.keras.callbacks.EarlyStopping('val_loss', patience=30, verbose=1),
tf.keras.callbacks.ReduceLROnPlateau('val_loss', patience=3, factor=0.1)]
hist = model.fit(x = x_train,
y = y_train,
epochs=65,
batch_size=128,
validation_data=(x_val, y_val),
callbacks=cbs,
verbose = 0)
 log_loss = min(hist.history['val_loss'])
 print('-'*20)
 print(f"> Log loss: {log_loss}")
 print('-'*20)
 global best_loss
if log_loss < best_loss:
model.save(path_best_model)
 best_loss = log_loss
del model
gc.collect()
K.clear_session()
return log_loss<train_on_grid>
for category in classes:
path = os.path.join(directory,category)
 class_num = classes.index(category)
 for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
 new_img = cv2.resize(img_array,(img_size2,img_size1))
training_data.append([
new_img,class_num])
 print('path :', path)
 print('img :', img)
 print('img_array:', img_array)
 print('img_array_shape:', img_array.shape)
 print('new_img :', new_img)
 print('new_img_shape:', new_img.shape)
 print('class_num:', class_num)
 break
search_result = skopt.gp_minimize(func=fitness,
dimensions=dimensions,
acq_func='EI',
n_calls=N_TRIALS,
random_state=SEED,
x0=default_params )<find_best_params>
training_data = []
i = 0
print('optimal hyper-parameters')
 print()
print(f'dense_units 3: {search_result.x[0]}')
 print(f'activation: {search_result.x[1]}')
 print(f'dropout 1: {search_result.x[2]}')
 print(f'dropout 2: {search_result.x[3]}')
 print(f'dropout 3: {search_result.x[4]}')
 print(f'dropout 4: {search_result.x[5]}')
 print(f'Look ahead: {search_result.x[6]}')
<create_dataframe>
testing_data = []
i = 0
def create_testing_data() :
for img in os.listdir(test_directory):
img_array = cv2.imread(os.path.join(test_directory,img),cv2.IMREAD_GRAYSCALE)
 new_img = cv2.resize(img_array,(img_size2,img_size1))
ret, bin_img = cv2.threshold(new_img, 128, 255, cv2.THRESH_BINARY)
 testing_data.append([img,
bin_img] )
pd.DataFrame(sorted(zip(search_result.func_vals, search_result.x_iters)) , index=np.arange(N_TRIALS), columns=['score', 'params'] )<choose_model_class>
for img in os.listdir(test_directory):
img_array = cv2.imread(os.path.join(test_directory,img),cv2.IMREAD_GRAYSCALE)
 new_img = cv2.resize(img_array,(img_size2,img_size1))
testing_data.append([img,
new_img])
 print('test_directory :', test_directory)
 print('img :', img)
 print('img_array:', img_array)
 print('img_array_shape:', img_array.shape)
 print('new_img :', new_img)
 print('new_img_shape:', new_img.shape)
 break
model = create_model(*search_result.x)
 model.summary()<compute_test_metric>
testing_data = []
i = 0
metrics = []
for _target in target_cols:
metrics.append(log_loss(targets.loc[:, _target], oof.loc[:, _target]))
print(f'OOF Metric: {np.mean(metrics ).round(8)}' )<compute_test_metric>
start = time.time()
create_training_data()
print('Elapsed_time: ', time.time() -start, '[sec]' )
oof.loc[train_x['cp_type']==1, target_cols] = 0
metrics = []
for _target in target_cols:
metrics.append(log_loss(targets.loc[:, _target], oof.loc[:, _target]))
print(f'OOF Metric(with post-processing): {np.mean(metrics ).round(8)}' )<save_to_csv>
random.shuffle(training_data )
subm.loc[test_x['cp_type']==1, target_cols] = 0
subm.to_csv('submission.csv', index=False )<load_from_csv>
x = []
y = []
def load_dataset(root):
csv = pd.read_csv(os.path.join(root, "train.csv"))
data = {}
for i in csv.index:
key = csv["image_id"][i]
bbox = json.loads(csv["bbox"][i])
 bbox = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3], 0.0]
if key in data:
data[key].append(bbox)
 else:
data[key] = [bbox]
return sorted(
 [(k, os.path.join(root, "train", k + ".jpg"), v)for k, v in data.items() ],
key=lambda x: x[0] 
)
 def load_image(path):
with open(path, "rb")as f:
buf = f.read()
return mx.image.imdecode(buf)
 def get_batches(dataset, batch_size, width=512, height=512, net=None, ctx=mx.cpu()):
batches = len(dataset)// batch_size
sampler = Sampler(dataset, width, height, net)
 stack_fn = [gcv.data.batchify.Stack() ]
pad_fn = [gcv.data.batchify.Pad(pad_val=-1)]
if net is None:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn + pad_fn))
else:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn * 6 + pad_fn))
with Pool(cpu_count() * 2)as p:
for i in range(batches):
start = i * batch_size
samples = p.map(sampler, range(start, start + batch_size))
batch = batchify_fn(samples)
 yield [x.as_in_context(ctx)for x in batch]
def gauss_blur(image, level):
return cv2.blur(image,(level * 2 + 1, level * 2 + 1))
def gauss_noise(image):
for i in range(image.shape[2]):
c = image[:, :, i]
diff = 255 - c.max() ;
noise = np.random.normal(0, random.randint(1, 6), c.shape)
 noise =(noise - noise.min())/(noise.max() - noise.min())
 noise = diff * noise
image[:, :, i] = c + noise.astype(np.uint8)
 return image
class YOLO3TrainTransform:
def __init__(self, width, height, net, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225), **kwargs):
self._width = width
self._height = height
self._mean = mean
self._std = std
self._fake_x = mx.nd.zeros(( 1, 3, height, width))
net = copy.deepcopy(net)
 net.collect_params().reset_ctx(None)
 with mx.autograd.train_mode() :
_, self._anchors, self._offsets, self._feat_maps, _, _, _, _ = net(self._fake_x)
 self._target_generator = gcv.model_zoo.yolo.yolo_target.YOLOV3PrefetchTargetGenerator(
 num_class=len(net.classes), **kwargs)
 def __call__(self, img, label):
if np.random.uniform(0, 1)> 0.5:
img, expand = gcv.data.transforms.image.random_expand(img, max_ratio=1.5, fill=114, keep_ratio=False)
 bbox = gcv.data.transforms.bbox.translate(label, x_offset=expand[0], y_offset=expand[1])
 else:
img, bbox = img, label
choice3 = random.randint(0,2)
 if choice3 == 1:
thre = random.randint(20,24)
 for x in range(16):
for y in range(16):
img[64*x:64*x+thre,64*y:64*y+thre]=0
else:
for box in bbox:
area =(box[3] - box[1])*(box[2] - box[0])
 if area > 1000 and random.random() >0.7 and area < 25000:
x1 = int(box[0]+(box[2]-box[0])*0.35)
 x2 = int(box[2]-(box[2]-box[0])*0.3)
 y1 = int(box[1]+(box[3]-box[1])*0.35)
 y2 = int(box[3]-(box[3]-box[1])*0.3)
 img[y1:y2,x1:x2] = random.randint(0,144)
 h, w, _ = img.shape
bbox, crop = gcv.data.transforms.experimental.bbox.random_crop_with_constraints(bbox,(w, h))
x0, y0, w, h = crop
img = mx.image.fixed_crop(img, x0, y0, w, h)
 h, w, _ = img.shape
interp = np.random.randint(0, 5)
 img = gcv.data.transforms.image.imresize(img, self._width, self._height, interp=interp)
 bbox = gcv.data.transforms.bbox.resize(bbox,(w, h),(self._width, self._height))
h, w, _ = img.shape
img, flips = gcv.data.transforms.image.random_flip(img, px=0.5, py=0.5)
 bbox = gcv.data.transforms.bbox.flip(bbox,(w, h), flip_x=flips[0], flip_y=flips[1])
 img = gcv.data.transforms.experimental.image.random_color_distort(img)
 img = mx.nd.image.to_tensor(img)
 img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
 gt_bboxes = mx.nd.array(bbox[np.newaxis, :, :4])
 gt_ids = mx.nd.array(bbox[np.newaxis, :, 4:5])
 gt_mixratio = mx.nd.array(bbox[np.newaxis, :, -1:])
 objectness, center_targets, scale_targets, weights, class_targets = self._target_generator(
 self._fake_x, self._feat_maps, self._anchors, self._offsets,
gt_bboxes, gt_ids, gt_mixratio)
 return(img, objectness[0], center_targets[0], scale_targets[0], weights[0],
class_targets[0], gt_bboxes[0])
 class Sampler:
def __init__(self, dataset, width, height, net=None, **kwargs):
self._dataset = dataset
if net is None:
self._training_mode = False
self._transform = gcv.data.transforms.presets.yolo.YOLO3DefaultValTransform(width, height, **kwargs)
 else:
self._training_mode = True
self._transform = YOLO3TrainTransform(width, height, net, **kwargs)
 def __call__(self, idx):
if self._training_mode:
raw, bboxes = self._load_mixup(idx)
 raw = raw.asnumpy()
blur = random.randint(0, 3)
 if blur > 0:
raw = gauss_blur(raw, blur)
 raw = gauss_noise(raw)
 h, w, _ = raw.shape
rot = random.randint(0, 3)
 if rot > 0:
raw = np.rot90(raw, k=rot)
 if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = w - raw_bboxes[:, [2, 0]]
elif rot == 2:
bboxes[:, [0, 1, 2, 3]] = np.array([[w, h, w, h]])- bboxes[:, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = h - raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = raw_bboxes[:, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[:, 0] = np.min(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 1] = np.min(raw_bboxes[:, [1, 3]], axis=1)
 bboxes[:, 2] = np.max(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 3] = np.max(raw_bboxes[:, [1, 3]], axis=1)
 raw = mx.nd.array(raw)
 else:
raw = load_image(self._dataset[idx][1])
 bboxes = np.array(self._dataset[idx][2])
 res = self._transform(raw, bboxes)
 return [mx.nd.array(x)for x in res]
def _load_mixup(self, idx1):
r = random.gauss(0.5, 0.5 / 1.96)
 if r > 0.0:
raw1 = load_image(self._dataset[idx1][1])
 bboxes1 = np.array(self._dataset[idx1][2])
 if r >= 1.0:
return raw1, np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), 1.0)])
 idx2 = random.randint(0, len(self._dataset)- 1)
 raw2 = load_image(self._dataset[idx2][1])
 bboxes2 = np.array(self._dataset[idx2][2])
 if r <= 0.0:
return raw2, np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0)])
 h = max(raw1.shape[0], raw2.shape[0])
 w = max(raw1.shape[1], raw2.shape[1])
 mix_raw = mx.nd.zeros(shape=(h, w, 3), dtype="float32")
 mix_raw[:raw1.shape[0], :raw1.shape[1], :] += raw1.astype("float32")* r
mix_raw[:raw2.shape[0], :raw2.shape[1], :] += raw2.astype("float32")*(1.0 - r)
 mix_bboxes = np.vstack([
np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), r)]),
np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0 - r)])
 ])
 return mix_raw.astype("uint8"), mix_bboxes<load_pretrained>
for features, label in training_data:
x.append(features)
 y.append(label )
def load_model(path, ctx=mx.cpu()):
net = gcv.model_zoo.yolo3_darknet53_custom(["wheat"], pretrained_base=False)
 net.set_nms(post_nms=150)
 net.load_parameters(path, ctx=ctx)
 return net 
<normalization>
print('features: ', x[0])
 print('label : ', y[0] )
def inference(path):
raw = load_image(path)
 rh, rw, _ = raw.shape
classes_list = []
scores_list = []
bboxes_list = []
for _ in range(5):
img, flips = gcv.data.transforms.image.random_flip(raw, px=0.5, py=0.5)
 x, _ = gcv.data.transforms.presets.yolo.transform_test(img, short=img_s)
 _, _, xh, xw = x.shape
rot = random.randint(0, 3)
 if rot > 0:
x = np.rot90(x.asnumpy() , k=rot, axes=(2, 3))
classes, scores, bboxes = model(mx.nd.array(x, ctx=context))
if rot > 0:
if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = xh - raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = raw_bboxes[0, :, [2, 0]]
elif rot == 2:
bboxes[0, :, [0, 1, 2, 3]] = mx.nd.array([[xw], [xh], [xw], [xh]], ctx=context)- bboxes[0, :, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = xw - raw_bboxes[0, :, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[0, :, 0] = raw_bboxes[0, :, [0, 2]].min(axis=0)
 bboxes[0, :, 1] = raw_bboxes[0, :, [1, 3]].min(axis=0)
 bboxes[0, :, 2] = raw_bboxes[0, :, [0, 2]].max(axis=0)
 bboxes[0, :, 3] = raw_bboxes[0, :, [1, 3]].max(axis=0)
 bboxes[0, :, :] = gcv.data.transforms.bbox.flip(bboxes[0, :, :],(xw, xh), flip_x=flips[0], flip_y=flips[1])
 bboxes[0, :, 0::2] =(bboxes[0, :, 0::2] /(xw - 1)).clip(0.0, 1.0)
 bboxes[0, :, 1::2] =(bboxes[0, :, 1::2] /(xh - 1)).clip(0.0, 1.0)
 classes_list.append([
int(classes[0, i].asscalar())for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 scores_list.append([
scores[0, i].asscalar() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes_list.append([
bboxes[0, i].asnumpy().tolist() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes, scores, classes = weighted_boxes_fusion(bboxes_list, scores_list, classes_list)
 bboxes[:, 0::2] *= rw - 1
bboxes[:, 1::2] *= rh - 1
return bboxes, scores, classes<load_pretrained>
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=50 )
rounds = 3
max_epochs = 3
learning_rate = 0.001
batch_size = 8
img_s = 512
threshold = 0.1
context = mx.gpu()
print("Loading model...")
 model = load_model(".. /input/yolov3/global-wheat-yolo3-darknet53.params", ctx=context)
 print("Loading training set...")
 dataset = load_dataset("/kaggle/input/global-wheat-detection")
 print("Loading test images...")
 test_images = [ 
(os.path.join(dirname, filename), os.path.splitext(filename)[0])
 for dirname, _, filenames in os.walk('/kaggle/input/global-wheat-detection/test')for filename in filenames
]
best_score = 0.0
for _ in range(rounds):
print("Pseudo labaling...")
 pseudo_set = []
for path, image_id in test_images:
print(path)
 bboxes, scores, classes = inference(path)
 label = [
[round(x)for x in bboxes[i].tolist() ] + [0.0] for i in range(classes.shape[0])
 if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
]
if len(label)> 0:
pseudo_set.append(( image_id, path, label))
if len(pseudo_set)> 10:
print("Submitting Mode")
 split = int(len(dataset)* 0.9)
 training_set = dataset[:split] + pseudo_set
validation_set = dataset[split:]
else:
print("Saving Mode")
 training_set = pseudo_set
validation_set = pseudo_set
print("Training set: ", len(training_set))
print("Validation set: ", len(validation_set))
print("Re-training...")
 trainer = mx.gluon.Trainer(model.collect_params() , "Nadam", {
"learning_rate": learning_rate
})
 metrics = [gcv.utils.metrics.VOCMApMetric(iou_thresh=iou)for iou in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]]
for epoch in range(max_epochs):
ts = time.time()
random.shuffle(training_set)
 training_total_L = 0.0
training_batches = 0
for x, objectness, center_targets, scale_targets, weights, class_targets, gt_bboxes in get_batches(training_set, batch_size, width=img_s, height=img_s, net=model, ctx=context):
training_batches += 1
with mx.autograd.record() :
obj_loss, center_loss, scale_loss, cls_loss = model(x, gt_bboxes, objectness, center_targets, scale_targets, weights, class_targets)
 L = obj_loss + center_loss + scale_loss + cls_loss
L.backward()
trainer.step(x.shape[0])
 training_batch_L = mx.nd.mean(L ).asscalar()
if training_batch_L != training_batch_L:
raise ValueError()
training_total_L += training_batch_L
print("[Epoch %d Batch %d] batch_loss %.10f average_loss %.10f elapsed %.2fs" %(
 epoch, training_batches, training_batch_L, training_total_L / training_batches, time.time() - ts 
))
training_avg_L = training_total_L / training_batches
for metric in metrics:
metric.reset()
for x, label in get_batches(validation_set, batch_size, width=img_s, height=img_s, ctx=context):
classes, scores, bboxes = model(x)
 for metric in metrics:
metric.update(
 bboxes,
classes.reshape(( 0, -1)) ,
scores.reshape(( 0, -1)) ,
label[:, :, :4],
label[:, :, 4:5].reshape(( 0, -1)) 
)
 score = mx.nd.array([metric.get() [1] for metric in metrics], ctx=context ).mean()
print("[Epoch %d] training_loss %.10f validation_score %.10f best_score %.10f duration %.2fs" %(
 epoch + 1, training_avg_L, score.asscalar() , best_score, time.time() - ts 
))
if score.asscalar() > best_score:
best_score = score.asscalar()
model.save_parameters("global-wheat-yolo3-darknet53.params")
 print("Loading re-trained model...")
 model = load_model("global-wheat-yolo3-darknet53.params", ctx=context)
 print("Inference...")
 results = []
for path, image_id in test_images:
print(path)
 bboxes, scores, classes = inference(path)
 indexes = np.where(scores > 0.05)[0]
boxes = boxes[indexes]
bboxes[:, 2:4] -= bboxes[:, 0:2]
results.append({
"image_id": image_id,
"PredictionString": " ".join([
" ".join([str(x)for x in [scores[i]] + [round(x)for x in bboxes[i].tolist() ]])
 for i in range(classes.shape[0])
 if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
])
 })
 pd.DataFrame(results, columns=['image_id', 'PredictionString'] ).to_csv('submission.csv', index=False )<import_modules>
Y_train = np_utils.to_categorical(y_train,num_classes=10)
 Y_test = np_utils.to_categorical(y_test,num_classes=10 )
import pandas as pd<import_modules>
model = Sequential()
import pandas as pd<load_from_csv>
model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(img_size1,img_size2,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=(3,3),activation='relu',padding='same'))
model.add(BatchNormalization(axis = 3))
model.add(MaxPooling2D(pool_size=(2,2),padding='same'))
model.add(Dropout(0.3))
csv = pd.read_csv('.. /input/j-h-h-h-h-h/psedu.csv')
 csv[csv['image_id']=='f5a1f0358']<set_options>
model.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))
model.add(BatchNormalization(axis = 3))
model.add(MaxPooling2D(pool_size=(2,2),padding='same'))
model.add(Dropout(0.3))
plt.imshow(img )<define_variables>
model.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))
model.add(BatchNormalization(axis = 3))
model.add(MaxPooling2D(pool_size=(2,2),padding='same'))
model.add(Dropout(0.5))
NMS_IOU_THR = 0.6
NMS_CONF_THR = 0.5
best_iou_thr = 0.6
best_skip_box_thr = 0.43
best_final_score = 0
best_score_threshold = 0
EPO = 15
WEIGHTS = '.. /input/minedd/best_yolov5x_fold0x0.pt'
CONFIG = '.. /input/wheatyolov5/yolov5x.yaml'
DATA = '.. /input/configyolo5/wheat0.yaml'
is_TEST = len(os.listdir('.. /input/global-wheat-detection/test/')) >11
is_AUG = True
is_ROT = True
VALIDATE = True
PSEUDO = True
marking = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking[column] = bboxs[:,i]
marking.drop(columns=['bbox'], inplace=True )<feature_engineering>
model.add(Flatten())
 model.add(Dense(units = 512,activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(units = 128,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(10,activation='softmax'))
def convertTrainLabel() :
df = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
df[column] = bboxs[:,i]
df.drop(columns=['bbox'], inplace=True)
 df['x_center'] = df['x'] + df['w']/2
df['y_center'] = df['y'] + df['h']/2
df['classes'] = 0
df = df[['image_id','x', 'y', 'w', 'h','x_center','y_center','classes']]
index = list(set(df.image_id))
source = 'train'
if True:
for fold in [0]:
val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]
for name, mini in tqdm(df.groupby('image_id')) :
path2save = 'val2017/' if name in val_index else 'train2017/'
os.makedirs('convertor/fold{}/labels/'.format(fold)+path2save, exist_ok=True)
 with open('convertor/fold{}/labels/'.format(fold)+path2save+name+".txt", 'w+')as f:
row = mini[['classes','x_center','y_center','w','h']].astype(float ).values
row = row/1024
row = row.astype(str)
 for j in range(len(row)) :
text = ' '.join(row[j])
 f.write(text)
 f.write("
")
 os.makedirs('convertor/fold{}/images/{}'.format(fold,path2save), exist_ok=True)
 sh.copy(".. /input/global-wheat-detection/{}/{}.jpg".format(source,name),'convertor/fold{}/images/{}/{}.jpg'.format(fold,path2save,name)) 
<concatenate>
model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam' )
def run_wbf(boxes, scores, image_size=1024, iou_thr=0.5, skip_box_thr=0.7, weights=None):
labels = [np.zeros(score.shape[0])for score in scores]
boxes = [box/(image_size)for box in boxes]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size)
 return boxes, scores, labels
def TTAImage(image, index):
image1 = image.copy()
if index==0:
rotated_image = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image
elif index==1:
rotated_image2 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image2 = cv2.rotate(rotated_image2, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image2
elif index==2:
rotated_image3 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image3
elif index == 3:
return image1
def rotBoxes90(boxes, im_w, im_h):
ret_boxes =[]
for box in boxes:
x1, y1, x2, y2 = box
x1, y1, x2, y2 = x1-im_w//2, im_h//2 - y1, x2-im_w//2, im_h//2 - y2
x1, y1, x2, y2 = y1, -x1, y2, -x2
x1, y1, x2, y2 = int(x1+im_w//2), int(im_h//2 - y1), int(x2+im_w//2), int(im_h//2 - y2)
 x1a, y1a, x2a, y2a = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
 ret_boxes.append([x1a, y1a, x2a, y2a])
 return np.array(ret_boxes)
 def detect1Image(img, img0, model, device, aug):
img = img.transpose(2,0,1)
 img = torch.from_numpy(img ).to(device)
 img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=aug)[0]
pred = non_max_suppression(pred, NMS_CONF_THR, NMS_IOU_THR, merge=True, classes=None, agnostic=False)
 boxes = []
scores = []
for i, det in enumerate(pred):
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape ).round()
for *xyxy, conf, cls in det:
boxes.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])
 scores.append(conf)
 return np.array(boxes), np.array(scores )<normalization>
callbacks = [EarlyStopping(monitor='val_acc',patience=5)]
@jit(nopython=True)
 def calculate_iou(gt, pr, form='pascal_voc')-> float:

 if form == 'coco':
gt = gt.copy()
pr = pr.copy()
gt[2] = gt[0] + gt[2]
gt[3] = gt[1] + gt[3]
pr[2] = pr[0] + pr[2]
pr[3] = pr[1] + pr[3]
dx = min(gt[2], pr[2])- max(gt[0], pr[0])+ 1
if dx < 0:
return 0.0
dy = min(gt[3], pr[3])- max(gt[1], pr[1])+ 1
if dy < 0:
return 0.0
overlap_area = dx * dy
union_area =(
(gt[2] - gt[0] + 1)*(gt[3] - gt[1] + 1)+ 
(pr[2] - pr[0] + 1)*(pr[3] - pr[1] + 1)-
overlap_area 
)
 return overlap_area / union_area
@jit(nopython=True)
 def find_best_match(gts, pred, pred_idx, threshold = 0.5, form = 'pascal_voc', ious=None)-> int:

 best_match_iou = -np.inf
best_match_idx = -1
for gt_idx in range(len(gts)) :
if gts[gt_idx][0] < 0:
continue
iou = -1 if ious is None else ious[gt_idx][pred_idx]
if iou < 0:
iou = calculate_iou(gts[gt_idx], pred, form=form)
 if ious is not None:
ious[gt_idx][pred_idx] = iou
if iou < threshold:
continue
if iou > best_match_iou:
best_match_iou = iou
best_match_idx = gt_idx
return best_match_idx
@jit(nopython=True)
 def calculate_precision(gts, preds, threshold = 0.5, form = 'coco', ious=None)-> float:

 n = len(preds)
 tp = 0
fp = 0
for pred_idx in range(n):
best_match_gt_idx = find_best_match(gts, preds[pred_idx], pred_idx,
threshold=threshold, form=form, ious=ious)
 if best_match_gt_idx >= 0:
tp += 1
gts[best_match_gt_idx] = -1
else:
fp += 1
fn =(gts.sum(axis=1)> 0 ).sum()
return tp /(tp + fp + fn)
 @jit(nopython=True)
 def calculate_image_precision(gts, preds, thresholds =(0.5,), form = 'coco')-> float:

 n_threshold = len(thresholds)
 image_precision = 0.0
ious = np.ones(( len(gts), len(preds)))* -1
for threshold in thresholds:
precision_at_threshold = calculate_precision(gts.copy() , preds, threshold=threshold,
form=form, ious=ious)
 image_precision += precision_at_threshold / n_threshold
return image_precision
iou_thresholds = numba.typed.List()
for x in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]:
iou_thresholds.append(x)
 def validate() :
source = 'convertor/fold0/images/val2017'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = WEIGHTS
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 results = []
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 records = marking[marking['image_id'] == image_id]
gtboxes = records[['x', 'y', 'w', 'h']].values
gtboxes = gtboxes.astype(np.int32 ).clip(min=0, max=1024)
 gtboxes[:, 2] = gtboxes[:, 0] + gtboxes[:, 2]
gtboxes[:, 3] = gtboxes[:, 1] + gtboxes[:, 3]
result = {
'image_id': image_id,
'pred_enboxes': enboxes,
'pred_enscores': enscores,
'gt_boxes': gtboxes,
}
results.append(result)
 return results
def calculate_final_score(all_predictions, iou_thr, skip_box_thr, score_threshold):
final_scores = []
for i in range(len(all_predictions)) :
gt_boxes = all_predictions[i]['gt_boxes'].copy()
enboxes = all_predictions[i]['pred_enboxes'].copy()
enscores = all_predictions[i]['pred_enscores'].copy()
image_id = all_predictions[i]['image_id']
pred_boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 pred_boxes = pred_boxes.astype(np.int32 ).clip(min=0, max=1024)
 indexes = np.where(scores>score_threshold)
 pred_boxes = pred_boxes[indexes]
scores = scores[indexes]
image_precision = calculate_image_precision(gt_boxes, pred_boxes,thresholds=iou_thresholds,form='pascal_voc')
 final_scores.append(image_precision)
 return np.mean(final_scores)
 def show_result(sample_id, preds, gt_boxes):
sample = cv2.imread(f'.. /input/global-wheat-detection/train/{sample_id}.jpg', cv2.IMREAD_COLOR)
 sample = cv2.cvtColor(sample, cv2.COLOR_BGR2RGB)
 fig, ax = plt.subplots(1, 1, figsize=(16, 8))
for pred_box in preds:
cv2.rectangle(
 sample, 
(pred_box[0], pred_box[1]), 
(pred_box[2], pred_box[3]), 
(220, 0, 0), 2 
)
 for gt_box in gt_boxes:
cv2.rectangle(
 sample, 
(gt_box[0], gt_box[1]), 
(gt_box[2], gt_box[3]), 
(0, 0, 220), 2 
)
 ax.set_axis_off()
ax.imshow(sample)
 ax.set_title("RED: Predicted | BLUE - Ground-truth" )<train_model>
batch_size = 50
n_epochs = 20
def log(text):
print(text)
 def optimize(space, all_predictions, n_calls=10):
@use_named_args(space)
 def score(**params):
log('-'*10)
 log(params)
 final_score = calculate_final_score(all_predictions, **params)
 log(f'final_score = {final_score}')
 log('-'*10)
 return -final_score
return gp_minimize(func=score, dimensions=space, n_calls=n_calls )<load_pretrained>
results = model.fit(x_train,Y_train,batch_size=batch_size,epochs=n_epochs,verbose=1,validation_data=(x_test,Y_test),callbacks=callbacks )
def makePseudolabel() :
source = '.. /input/global-wheat-detection/test/'
weights = WEIGHTS
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 path2save = 'train2017/'
if not os.path.exists('convertor/fold0/labels/'+path2save):
os.makedirs('convertor/fold0/labels/'+path2save)
 if not os.path.exists('convertor/fold0/images/{}'.format(path2save)) :
os.makedirs('convertor/fold0/images/{}'.format(path2save))
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=1024)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
indices = scores >= best_score_threshold
boxes = boxes[indices]
scores = scores[indices]
lineo = ''
for box in boxes:
x1, y1, w, h = box
xc, yc, w, h =(x1+w/2)/1024,(y1+h/2)/1024, w/1024, h/1024
lineo += '0 %f %f %f %f
'%(xc, yc, w, h)
 fileo = open('convertor/fold0/labels/'+path2save+image_id+".txt", 'w+')
 fileo.write(lineo)
 fileo.close()
sh.copy(".. /input/global-wheat-detection/test/{}.jpg".format(image_id),'convertor/fold0/images/{}/{}.jpg'.format(path2save,image_id)) 
<categorify>
preds = model.predict(np.array(testing_data[0][1] ).reshape(-1,img_size2,img_size1,1))
if PSEUDO or VALIDATE:
convertTrainLabel()<find_best_params>
model.save_weights('./driverdistraction_lr_weights.h5', overwrite=True )
if VALIDATE and is_TEST:
all_predictions = validate()

 for score_threshold in tqdm(np.arange(0, 1, 0.01), total=np.arange(0, 1, 0.01 ).shape[0]):
final_score = calculate_final_score(all_predictions, best_iou_thr, best_skip_box_thr, score_threshold)
 if final_score > best_final_score:
best_final_score = final_score
best_score_threshold = score_threshold
print('-'*30)
 print(f'[Best Score Threshold]: {best_score_threshold}')
 print(f'[OOF Score]: {best_final_score:.4f}')
 print('-'*30)
<load_pretrained>
model.save('./driverdistraction_lr_weights.h5' )
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)
 def detect() :
source = '.. /input/global-wheat-detection/test/'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = WEIGHTS
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 results = []
fig, ax = plt.subplots(5, 2, figsize=(30, 70))
count = 0
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=1024)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
indices = scores >= best_score_threshold
boxes = boxes[indices]
scores = scores[indices]
if count<10:
img_ = cv2.imread(path)
 for box, score in zip(boxes,scores):
cv2.rectangle(img_,(box[0], box[1]),(box[2]+box[0], box[3]+box[1]),(220, 0, 0), 2)
 cv2.putText(img_, '%.2f'%(score),(box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255), 2, cv2.LINE_AA)
 ax[count%5][count//5].imshow(img_)
 count+=1
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 return results<save_to_csv>
loaded_model = load_model('./driverdistraction_lr_weights.h5' )
results = detect()
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<import_modules>
test_data = np.array(testing_data[1001][1] ).reshape(-1,img_size2,img_size1,1 )
sys.path.insert(0, "/kaggle/input/weightedboxesfusion")
<define_variables>
preds = model.predict(test_data)
 preds
DATA_DIR = "/kaggle/input/global-wheat-detection"
MODELS_IN_DIR = "/kaggle/input/frcnnfoldthree"<load_from_csv>
preds= np.argmax(preds)
 preds
test_df = pd.read_csv(os.path.join(DATA_DIR, "sample_submission.csv"))
test_df.shape<feature_engineering>
classes = {0: "safe driving",
1: "texting - right",
2: "talking on the phone - right",
3: "texting - left",
4: "talking on the phone - left",
5: "operating the radio",
6: "drinking",
7: "reaching behind",
8: "hair and makeup",
9: "talking to passenger",
}
for key,value in classes.items() :
if preds==key:
predicted = value
predicted
class WheatDataset(Dataset):
def __init__(self, dataframe, image_dir, transforms=None):
super().__init__()
self.image_ids = dataframe['image_id'].unique()
self.df = dataframe
self.image_dir = image_dir
self.transforms = transforms
def __len__(self)-> int:
return len(self.image_ids)
 def __getitem__(self, idx: int):
image_id = self.image_ids[idx]
image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
records = self.df[self.df['image_id'] == image_id]
if self.transforms:
sample = {"image": image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id 
<choose_model_class>
x_test=[]
y_test=[]
for test_id, feature in testing_data:
x_test.append(feature)
 y_test.append(test_id )
def get_model() :

 backbone = resnet_fpn_backbone('resnet101', pretrained=False)
 model = FasterRCNN(backbone, num_classes=2)
 return model<load_pretrained>
print('features: ', x_test[0])
 print('test_id : ', y_test[0] )
DEVICE = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = get_model()
model.load_state_dict(torch.load(os.path.join(MODELS_IN_DIR, "best_model.pth")))
 model.eval()
model.to(DEVICE)
 1 == 1<categorify>
preds = model.predict(X_test )
def get_test_transforms() :
return A.Compose([
A.Resize(height=1024, width=1024, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<create_dataframe>
def create_submission(predictions, test_id, info):
result1 = pd.DataFrame(predictions, columns=['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
 result1.loc[:, 'img'] = pd.Series(test_id, index=result1.index)
 result1 = result1.sort_values(['img'])
 result1.to_csv(index=False)
 return result1
def collate_fn(batch):
return tuple(zip(*batch))
test_dataset = WheatDataset(test_df, os.path.join(DATA_DIR, "test"), get_test_transforms())
 test_data_loader = DataLoader(
 test_dataset,
batch_size=4,
shuffle=False,
num_workers=1,
drop_last=False,
collate_fn=collate_fn
)<define_variables>
info = '200824'
submission = create_submission(preds, y_test, info )
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 )<categorify>
now = datetime.datetime.now()
if not os.path.isdir('subm'):
os.mkdir('subm')
 suffix = info + '_' + str(now.strftime("%Y-%m-%d-%H-%M"))
sub_file = os.path.join('subm', 'submission_' + suffix + '.csv' )
class BaseWheatTTA:

 image_size = 1024
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [3,1]]
res_boxes[:, [1,3]] = boxes[:, [0,2]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes )<concatenate>
submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
submission.to_csv('submission.csv', index=False )