Kaggle competition: The Nature Conservancy Fisheries Monitoring
<SOS> metric: MulticlassLoss Kaggle data source: the-nature-conservancy-fisheries-monitoring<train_model>
print(tf.__version__ )
stack_clf = StackingClassifier(estimators=named_estimators, cv=kf, passthrough=False, n_jobs=-1)
 stack_clf.fit(X_train_transformed, y_train )<compute_train_metric>
TRAIN_PREFIX = '.. /input/fish-data/fish/train'
def load_annotations() :
boxes = dict()
for path in tqdm(glob('.. /input/fish-data/fish/boxes/*.json')) :
label = os.path.basename(path ).split('_', 1)[0]
with open(path)as src:
for annotation in json.load(src):
basename = os.path.basename(annotation['filename'])
 annotation['filename'] = os.path.join(
 TRAIN_PREFIX, label.upper() , basename)
 for rect in annotation['annotations']:
rect['x'] += rect['width'] / 2
rect['y'] += rect['height'] / 2
rect['class'] = label
if os.path.isfile(annotation['filename']):
boxes.setdefault(label, [] ).append(annotation)
 return boxes
def draw_boxes(annotation, rectangles=None, image_size=None):
def _draw(img, rectangles, scale_x, scale_y, color=(0, 255, 0)) :
for i, rect in enumerate(rectangles):
pt1 =(int(( rect['x'] - rect['width'] / 2)* scale_x),
int(( rect['y'] - rect['height'] / 2)* scale_y))
pt2 =(int(( rect['x'] + rect['width'] / 2)* scale_x),
int(( rect['y'] + rect['height'] / 2)* scale_y))
img = cv2.rectangle(img.copy() , pt1, pt2,
color=color, thickness=10)
 img = cv2.putText(img.copy() , annotation['annotations'][i]['class'], tuple(np.array(pt1)+[0,-7]), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 4)
 return img
def __draw(img, rectangles, scale_x, scale_y, color=(0, 255, 0)) :
for i, rect in enumerate(rectangles):
pt1 =(int(( rect['x'] - rect['width'] / 2)* scale_x),
int(( rect['y'] - rect['height'] / 2)* scale_y))
pt2 =(int(( rect['x'] + rect['width'] / 2)* scale_x),
int(( rect['y'] + rect['height'] / 2)* scale_y))
img = cv2.rectangle(img.copy() , pt1, pt2,
color=color, thickness=3)
 img = cv2.putText(img.copy() , counts['class'][int(rect['label'])] + ': ' + str(rect['label']), tuple(np.array(pt1)+[0,-7]), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 4)
 return img
scale_x, scale_y = 1., 1.
 img = cv2.imread(annotation['filename'], cv2.IMREAD_COLOR)[...,::-1]
if image_size is not None:
scale_x = 1.* image_size[0] / img.shape[1]
scale_y = 1.* image_size[1] / img.shape[0]
img = cv2.resize(img, image_size)
 img = _draw(img, annotation.get('annotations', []), scale_x, scale_y)
 if rectangles is not None:
img = __draw(img, rectangles, 1., 1.,(255, 0, 0))
return img
stack_acc = cross_val_score(stack_clf, X_train_transformed, y_train, scoring="accuracy", cv=kf, n_jobs=-1)
 stack_acc = np.round(np.median(stack_acc), 4)
 train_models.append(['Stacking Classifier', '', stack_acc] )<train_model>
boxes = load_annotations()
voting_clf = VotingClassifier(estimators=named_estimators, n_jobs=-1)
 voting_clf.fit(X_train_transformed, y_train )<compute_train_metric>
counts = pd.DataFrame(
 [(k, len(v)) for k, v in boxes.items() ],
columns=['class', 'count'])
 fish_classes = counts['class'].values
print(fish_classes)
 counts
voting_acc = cross_val_score(voting_clf, X_train_transformed, y_train, scoring="accuracy", cv=kf, n_jobs=-1)
 voting_acc = np.round(np.mean(voting_acc), 4)
 train_models.append(['Voting Classifier', '', voting_acc] )<create_dataframe>
IMG_HEIGHT = 736
IMG_WIDTH = 1184
features = VGG16(weights='imagenet',
include_top=False,
input_shape=(IMG_HEIGHT, IMG_WIDTH, 3))
for layer in features.layers[:-5]:
layer.trainable = False
feature_tensor = features.layers[-1].output
print(feature_tensor.shape )
pd.set_option('display.max_colwidth', -1)
 train_models_df = pd.DataFrame(train_models, columns=['Model', 'Best Paramas', 'Accuracy'])
 train_models_df<find_best_params>
FEATURE_SHAPE =(feature_tensor.shape[1],
feature_tensor.shape[2])
 print(FEATURE_SHAPE)
 GRID_STEP_H = IMG_HEIGHT / FEATURE_SHAPE[0]
GRID_STEP_W = IMG_WIDTH / FEATURE_SHAPE[1]
ANCHOR_WIDTH = 150.
 ANCHOR_HEIGHT = 150.
 ANCHOR_CENTERS = np.mgrid[GRID_STEP_H/2:IMG_HEIGHT:GRID_STEP_H,
GRID_STEP_W/2:IMG_WIDTH:GRID_STEP_W]
ANCHOR_CENTERS.shape
results = dict()
best_models = [best_lgr_clf, best_svc_clf, best_rf_clf, best_xgb_clf, stack_clf, voting_clf]
model_names = []
model_accuracy = []
for model in best_models:
test_accuracy_scores = cross_val_score(model, X_test_transformed, y_test, scoring="accuracy", cv=kf, n_jobs=-1)
 test_accuracy_scores = np.round(test_accuracy_scores,4)
 test_accuracy = np.round(np.mean(test_accuracy_scores),4)
 model_names.append(model.__class__.__name__)
 model_accuracy.append(test_accuracy )<find_best_params>
num_classes = counts.shape[0]
num_classes
best_model = best_models[np.argmax(model_accuracy)]
best_model<train_model>
from scipy.special import softmax
final_model = Pipeline([('pre_process', pre_process), 
('best_model', best_model)])
 final_model.fit(X_train, y_train )<load_from_csv>
def iou(rect, x_scale, y_scale, anchor_x, anchor_y,
anchor_w=ANCHOR_WIDTH, anchor_h=ANCHOR_HEIGHT):
rect_x1 =(rect['x'] - rect['width'] / 2)* x_scale
rect_x2 =(rect['x'] + rect['width'] / 2)* x_scale
rect_y1 =(rect['y'] - rect['height'] / 2)* y_scale
rect_y2 =(rect['y'] + rect['height'] / 2)* y_scale
anch_x1, anch_x2 = anchor_x - anchor_w / 2, anchor_x + anchor_w / 2
anch_y1, anch_y2 = anchor_y - anchor_h / 2, anchor_y + anchor_h / 2
dx =(min(rect_x2, anch_x2)- max(rect_x1, anch_x1))
dy =(min(rect_y2, anch_y2)- max(rect_y1, anch_y1))
intersection = dx * dy if(dx > 0 and dy > 0)else 0.
 anch_square =(anch_x2 - anch_x1)*(anch_y2 - anch_y1)
 rect_square =(rect_x2 - rect_x1)*(rect_y2 - rect_y1)
 union = anch_square + rect_square - intersection
return intersection / union
def encode_anchors(annotation, img_shape, iou_thr=0.5):
encoded = np.zeros(shape=(FEATURE_SHAPE[0],
FEATURE_SHAPE[1], 11), dtype=np.float32)
 x_scale = 1.* IMG_WIDTH / img_shape[1]
y_scale = 1.* IMG_HEIGHT / img_shape[0]
for rect in annotation['annotations']:
scores = []
label = fish_classes == rect['class']
for row in range(FEATURE_SHAPE[0]):
for col in range(FEATURE_SHAPE[1]):
anchor_x = ANCHOR_CENTERS[1, row, col]
anchor_y = ANCHOR_CENTERS[0, row, col]
score = iou(rect, x_scale, y_scale, anchor_x, anchor_y)
 scores.append(( score, anchor_x, anchor_y, row, col))
scores = sorted(scores, reverse=True)
 if scores[0][0] < iou_thr:
scores = [scores[0]]
else:
scores = [e for e in scores if e[0] > iou_thr]
for score, anchor_x, anchor_y, row, col in scores:
dx =(anchor_x - rect['x'] * x_scale)/ ANCHOR_WIDTH
dy =(anchor_y - rect['y'] * y_scale)/ ANCHOR_HEIGHT
dw =(ANCHOR_WIDTH - rect['width'] * x_scale)/ ANCHOR_WIDTH
dh =(ANCHOR_HEIGHT - rect['height'] * y_scale)/ ANCHOR_HEIGHT
encoded[row, col] = np.array([*label, 1., dx, dy, dw, dh])
 return encoded
def _sigmoid(x):
return 1./(1.+ np.exp(-x))
def decode_prediction(prediction, conf_thr=0.1):
rectangles = []
for row in range(FEATURE_SHAPE[0]):
for col in range(FEATURE_SHAPE[1]):
label = np.empty(6)
 label[0], label[1], label[2], label[3], label[4], label[5], conf, dx, dy, dw, dh = prediction[row, col]
conf = _sigmoid(conf)
 label = softmax(label)
 if conf > conf_thr:
anchor_x = ANCHOR_CENTERS[1, row, col]
anchor_y = ANCHOR_CENTERS[0, row, col]
rectangles.append({'x': anchor_x - dx * ANCHOR_WIDTH,
'y': anchor_y - dy * ANCHOR_HEIGHT,
'width': ANCHOR_WIDTH - dw * ANCHOR_WIDTH,
'height': ANCHOR_HEIGHT - dh * ANCHOR_HEIGHT,
'conf': conf,
'label': np.argmax(_sigmoid(label)) ,
'labels': label })
 return rectangles
def non_max_suppression(rectangles, max_output_size, iou_threshold=0.5):
if not rectangles:
return rectangles
boxes = [[r['y'],
r['x'],
r['y'] + r['height'],
r['x'] + r['width']] for r in rectangles]
scores = [r['conf'] for r in rectangles]
indices = tf.image.non_max_suppression(np.array(boxes),
np.array(scores),
max_output_size,
iou_threshold)
 return [rectangles[i] for i in indices]
test_data = pd.read_csv(".. /input/titanic/test.csv")
 test_data.info()<predict_on_test>
annotation = boxes['shark'][3]
encoded = encode_anchors(annotation,
img_shape=(IMG_HEIGHT, IMG_WIDTH),
iou_thr=0.1)
 decoded = decode_prediction(encoded, conf_thr=0.7)
 decoded = sorted(decoded, key = lambda e: -e['conf'])
 plt.figure(figsize=(6, 6), dpi=240)
 plt.imshow(draw_boxes(annotation, decoded))
plt.title('{} {}x{}'.format(
 annotation['filename'], img.shape[0], img.shape[1])) ;
predictions = final_model.predict(test_data )<prepare_output>
K = tf.keras.backend
def confidence_loss(y_true, y_pred):
conf_loss = K.binary_crossentropy(y_true[..., 6],
y_pred[..., 6],
from_logits=True)
 return conf_loss
def smooth_l1(y_true, y_pred):
abs_loss = K.abs(y_true[..., -4:] - y_pred[..., -4:])
 square_loss = 0.5 * K.square(y_true[..., -4:] - y_pred[..., -4:])
 mask = K.cast(K.greater(abs_loss, 1.) , 'float32')
 total_loss =(abs_loss - 0.5)* mask + 0.5 * square_loss *(1.- mask)
 return K.sum(total_loss, axis=-1)
 def classification_loss(y_tr, y_pr, alpha=0.25, gamma=2.0):

 y_true = y_tr[..., :6]
y_pred = y_pr[..., :6]
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(
 labels=y_true, logits=y_pred 
)
 probs = tf.nn.softmax(y_pred)
 alpha = tf.where(tf.equal(y_true, 1.0), alpha,(1.0 - alpha))
pt = tf.where(tf.equal(y_true, 1.0), probs, 1 - probs)
 loss = alpha * tf.pow(1.0 - pt, gamma)* cross_entropy
return tf.reduce_sum(loss, axis=-1)
 def class_loss(y_tr, y_pr):
y_true = y_tr[..., :6]
y_pred = y_pr[..., :6]
cross_entropy = K.categorical_crossentropy(y_true[..., :6],
y_pred[..., :6],
from_logits=True)
 return cross_entropy
def total_loss(y_true, y_pred, neg_pos_ratio=3):
batch_size = K.shape(y_true)[0]
y_true = K.reshape(y_true,(batch_size, -1, 11))
y_pred = K.reshape(y_pred,(batch_size, -1, 11))
cls_loss = classification_loss(y_true, y_pred)
 conf_loss = confidence_loss(y_true, y_pred)
 loc_loss = smooth_l1(y_true, y_pred)
 pos_conf_loss = K.sum(conf_loss * y_true[..., 6], axis=-1)
 pos_class_loss = K.sum(cls_loss * y_true[..., 6], axis=-1)
 pos_loc_loss = K.sum(loc_loss * y_true[..., 6], axis=-1)
 anchors = K.shape(y_true)[1]
num_pos = K.sum(y_true[..., 6], axis=-1)
 num_pos_avg = K.mean(num_pos)
 num_neg = K.min([neg_pos_ratio *(num_pos_avg)+ 1., K.cast(anchors, 'float32')])
 neg_conf_loss, _ = tf.nn.top_k(conf_loss *(1.- y_true[..., 6]),
k=K.cast(num_neg, 'int32'))
neg_conf_loss = K.sum(neg_conf_loss, axis=-1)
 total_conf_loss =(neg_conf_loss + pos_conf_loss)/(num_neg + num_pos + 1e-32)
 cls_loss = pos_class_loss /(num_pos + 1e-32)
 loc_loss = pos_loc_loss /(num_pos + 1e-32)
 return total_conf_loss + 0.5 * loc_loss + cls_loss
test_predictions = pd.DataFrame(test_data['PassengerId'])
 test_predictions['Survived'] = predictions.copy()
test_predictions.head()<save_to_csv>
def load_img(path, target_size=(IMG_WIDTH, IMG_HEIGHT)) :
img = cv2.imread(path, cv2.IMREAD_COLOR)[...,::-1]
img_shape = img.shape
img_resized = cv2.resize(img, target_size)
 return img_shape, tf.keras.applications.resnet_v2.preprocess_input(img_resized.astype(np.float32))
def data_generator(boxes, batch_size=32):
boxes = sum(boxes.values() , [])
 while True:
shuffle(boxes)
 for i in range(len(boxes)//batch_size):
X, y = [], []
for j in range(i*batch_size,(i+1)*batch_size):
img_shape, img = load_img(boxes[j]['filename'])
 y_ = encode_anchors(boxes[j], img_shape)
 y.append(y_)
 X.append(img)
 yield np.array(X), np.array(y )
test_predictions.to_csv("./submission.csv", index=False )<load_from_csv>
output = tf.keras.layers.BatchNormalization()(feature_tensor)
 output = tf.keras.layers.Conv2D(11,
kernel_size=(1, 1),
activation='linear',
kernel_regularizer='l2' )(output)
 model = tf.keras.models.Model(inputs=features.inputs, outputs=output)
house_data = pd.read_csv('.. /input/train.csv')
 house_data_test = pd.read_csv('.. /input/test.csv' )<drop_column>
lr=3e-3
train_parent=house_data
test_parent=house_data_test
house_data = house_data.drop('Id', axis=1)
 house_data_test = house_data_test.drop('Id', axis=1 )<create_dataframe>
def lr_exp_decay(epoch, lr):
k = 0.5
return lr * np.exp(-k*epoch)
 batch_size = 32
steps_per_epoch = sum(map(len, boxes.values()), 0)/ batch_size
gen = data_generator(boxes, batch_size=batch_size)
 checkpoint = tf.keras.callbacks.ModelCheckpoint(
 'fishdetector.hdf5',
monitor='loss',
verbose=47,
save_best_only=True,
save_weights_only=False,
mode='auto',
save_freq=10)
 adam = tf.keras.optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999)
 model.compile(optimizer=adam,
loss=total_loss,
metrics=[confidence_loss, classification_loss, smooth_l1])
threshold=0.4 * len(house_data)
 df=pd.DataFrame(len(house_data)- house_data.count() ,columns=['count'])
 df.index[df['count'] > threshold]<drop_column>
model.fit(gen,
steps_per_epoch=steps_per_epoch,
epochs=2,
callbacks=[checkpoint, tf.keras.callbacks.LearningRateScheduler(lr_exp_decay, verbose=1)] )
house_data = house_data.drop(['Alley', 'FireplaceQu', 'PoolQC', 'Fence', 'MiscFeature'], axis=1)
 house_data_test = house_data_test.drop(['Alley', 'FireplaceQu', 'PoolQC', 'Fence', 'MiscFeature'], axis=1 )<define_variables>
test_images = glob('.. /input/fish-data/test_stg1/test_stg1/*.jpg')[:4]
plt.figure(figsize=(6, 4 * len(test_images)) , dpi=240)
 for i, filename in enumerate(test_images):
_, sample_img = load_img(filename)
 pred = model.predict(np.array([sample_img,]))
decoded = decode_prediction(pred[0], conf_thr=0.1)
 decoded = non_max_suppression(decoded,
max_output_size=1,
iou_threshold=0.5)
 plt.subplot(len(test_images), 1, i + 1)
 img = draw_boxes({'filename': filename}, decoded,(IMG_WIDTH, IMG_HEIGHT))
plt.imshow(img)
 plt.title('Предсказание на тесте {}'.format(filename.split('/')[-1])) ;
house_data.select_dtypes(include=np.number ).columns
for col in('MSSubClass', 'LotFrontage', 'LotArea', 'OverallQual', 'OverallCond',
'YearBuilt', 'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'LowQualFinSF',
'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath',
'BedroomAbvGr', 'KitchenAbvGr', 'TotRmsAbvGrd', 'Fireplaces',
'GarageYrBlt', 'GarageCars', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF',
'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal',
'MoSold', 'YrSold'):
house_data[col] = house_data[col].fillna(0)
 house_data_test[col] = house_data_test[col].fillna('0' )<define_variables>
def make_predictions() :
ptable = pd.DataFrame(columns=['image', 'ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK','YFT'])
 for i, file in enumerate(tqdm(glob('.. /input/fish-data/test_stg1/test_stg1/*.jpg'))):
bn = os.path.basename(file)
 _, sample_img = load_img(file)
 pred = model.predict(np.array([sample_img,])) [0]
decoded = decode_prediction(pred, conf_thr=0.015)
 decoded = non_max_suppression(decoded,
max_output_size=1,
iou_threshold=0.5)
 decoded = decoded[0]['labels']
ptable.loc[i, 'image'] = bn
ptable.loc[i, 'ALB'] = decoded[5]
ptable.loc[i, 'BET'] = decoded[1]
ptable.loc[i, 'DOL'] = decoded[4]
ptable.loc[i, 'LAG'] = decoded[3]
ptable.loc[i, 'SHARK'] = decoded[0]
ptable.loc[i, 'YFT'] = decoded[2]
ptable.loc[i, 'NoF'] = 0.123081
ptable.loc[i, 'OTHER'] = 0.079142
i += 1
for j, file in enumerate(tqdm(glob('.. /input/fish-data/test_stg2/test_stg2/*.jpg'))):
bn = os.path.basename(file)
 bn = "test_stg2/" + bn
_, sample_img = load_img(file)
 pred = model.predict(np.array([sample_img,])) [0]
decoded = decode_prediction(pred, conf_thr=0.015)
 decoded = non_max_suppression(decoded,
max_output_size=1,
iou_threshold=0.5)
 decoded = decoded[0]['labels']
ptable.loc[i + j, 'image'] = bn
ptable.loc[i + j, 'ALB'] = decoded[5]
ptable.loc[i + j, 'BET'] = decoded[1]
ptable.loc[i + j, 'DOL'] = decoded[4]
ptable.loc[i + j, 'LAG'] = decoded[3]
ptable.loc[i + j, 'SHARK'] = decoded[0]
ptable.loc[i + j, 'YFT'] = decoded[2]
ptable.loc[i + j, 'NoF'] = 0.123081
ptable.loc[i + j, 'OTHER'] = 0.079142
return ptable
<filter><EOS> 
pred_table = make_predictions()
pred_table.to_csv("neto_submit.csv", index=False)
 print(os.listdir("./"))