Skip to content

Instantly share code, notes, and snippets.

View mohitprajapati's full-sized avatar

Mohit Kumar mohitprajapati

View GitHub Profile
callbacks = [
ReduceLROnPlateau(verbose=1),
EarlyStopping(patience=3, verbose=1),
ModelCheckpoint('/content/drive/MyDrive/Casestudy2/train/new_checkpoints/yolov3_train_{epoch}.tf',
verbose=1, save_weights_only=True),
TensorBoard(log_dir='/content/drive/MyDrive/Casestudy2/train/new_logs')
]
model = Yolo_V3(size=416,channels=3,training=True,classes=4)
pretrained_model = Yolo_V3(size=416,channels=3,classes=80,training=True)
pretrained_model.load_weights(WEIGHTS_PATH)
model.get_layer('yolo_darknet').set_weights(pretrained_model.get_layer('yolo_darknet').get_weights())
Freeze_All(model.get_layer('yolo_darknet'))
def Yolo_Loss(anchors, classes=80, ignore_thresh=0.5):
''' Loss function for YOLOV3 '''
def yolo_loss(y_true, y_pred):
# 1. transform all pred outputs
# y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
pred_box, pred_obj, pred_class, pred_xywh = yolo_boxes(y_pred, anchors, classes)
pred_xy = pred_xywh[..., 0:2]
pred_wh = pred_xywh[..., 2:4]
# 2. transform all true outputs
def yolo_boxes(pred, anchors, classes):
''' Function to get absolute coordinates of bounding boxes and objectness and class probs'''
# pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...classes))
Grd_Siz = tf.shape(pred)[1:3]
Box_xy, Box_wh, Objectness, Class_Prob = tf.split(
pred, (2, 2, 1, classes), axis=-1)
#print(class_probs)
Box_xy = tf.sigmoid(Box_xy)
Objectness = tf.sigmoid(Objectness)
Class_Prob = tf.sigmoid(Class_Prob)
def Create_TFrecord(path,xml,class_map,data,augmentation):
''' creating tf records '''
images = os.path.join(path,'images',xml['filename'])
img_raw = open(images,'rb').read()
img = cv2.imread(images)
width = int(xml['size']['width'])
height = int(xml['size']['height'])
if data == 'train' and augmentation:
# for train dataset and if augmentation
augseq = iaa.Sequential([
# stackoverflow for parsing the XML files
files = [file for file in os.listdir('/content/train/India/annotations/xmls')]
cnt_dmg= [] # list to store the counts for the demage
for file in files:
xmlfile = open('/content/train/India/annotations/xmls/' + file)
tree = ET.parse(xmlfile)
root = tree.getroot()
for obj in root.iter('object'):
dmg_typ = obj.find('name').text
cnt_dmg.append(dmg_typ)
for i in range(len(label_count)-1):
f1_state_terminal[i]/=273
print("The Combined Average F1 scores of train Terminals ",f1_state_terminal) # order available, passive, charging, offline, down
for i in range(len(label_count)-1):
f1_state_terminal_test[i]/=273
print("The Combined Average F1 scores of test Terminals ",f1_state_terminal_test) # order available, passive, charging, offline,down
f1_final=0
# one hot encoding
week_number_ohe= pd.get_dummies(X['Week_number'])
week_number_test_ohe=pd.get_dummies(X_test['Week_number'])
print(week_number_ohe.shape)
print(week_number_test_ohe.shape)
# Is weekend
def IsWeekEnd(number):
if number in [5,6]: # if staurday or sunday return 1
return 1
else: return 0 # weekdays=6
weekend=X['Week_number'].apply(IsWeekEnd)
weekend_test=X_test['Week_number'].apply(IsWeekEnd)
print(weekend.shape)
print(weekend_test.shape)
### DEFINE MODEL STRUCTURES ###
def T2V_NN(t2v_dim, dim):
inp = layers.Input(shape=(dim,1))
x = T2V(t2v_dim)(inp)
x = layers.LSTM(5, activation='tanh', return_sequences=True)(x)
x = layers.LSTM(5, activation='tanh')(x)