Kaggle competition: Lyft 3D Object Detection for Autonomous Vehicles
<SOS> metric: Lyft3DObjectDetectionAP Kaggle data source: 3d-object-detection-for-autonomous-vehicles<drop_column>
fn = '.. /input/lyft3d-mask-test-data/model_preds.gif'
Image(filename=fn )
train.drop(['Country'], axis=1, inplace=True)
 train.drop(['Province'], axis=1, inplace=True )<drop_column>
!pip install lyft-dataset-sdk -q
!pip install moviepy
test.drop(['Country'], axis=1, inplace=True)
 test.drop(['Province'], axis=1, inplace=True )<choose_model_class>
os.environ["OMP_NUM_THREADS"] = "1"
%matplotlib inline
regressor = DecisionTreeRegressor(random_state = 0 )<choose_model_class>
classes = ["car", "motorcycle", "bus", "bicycle", "truck", "pedestrian", "other_vehicle", "animal", "emergency_vehicle"]
train_dataset = LyftDataset(data_path='.', json_path='.. /input/3d-object-detection-for-autonomous-vehicles/train_data', verbose=True )
from sklearn.metrics import mean_squared_error, mean_absolute_error 
<prepare_x_and_y>
class_heights = {'animal':0.51,'bicycle':1.44,'bus':3.44,'car':1.72,'emergency_vehicle':2.39,'motorcycle':1.59,
'other_vehicle':3.23,'pedestrian':1.78,'truck':3.44}
level5data = LyftDataset(data_path='.', json_path='.. /input/3d-object-detection-for-autonomous-vehicles/test_data', verbose=True )
x = train[['Countries','State','dayofweek','month','dayofyear','weekofyear']]
y1 = train[['Confirmed']]
y2 = train[['Fatalities']]
x_test = test[['Countries','State','dayofweek','month','dayofyear','weekofyear']]<predict_on_test>
voxel_size =(0.4, 0.4, 1.5)
 z_offset = -2.0
bev_shape =(336, 336, 3)
 box_scale = 0.8
regressor.fit(x,y1)
 predict_1 = regressor.predict(x_test)
 predict_1 = pd.DataFrame(predict_1)
 predict_1.columns = ["Confirmed_predict"]<predict_on_test>
records = [(level5data.get('sample', record['first_sample_token'])['timestamp'], record)for record in level5data.scene]
entries = []
for start_time, record in sorted(records):
start_time = level5data.get('sample', record['first_sample_token'])['timestamp'] / 1000000
token = record['token']
name = record['name']
date = datetime.utcfromtimestamp(start_time)
 host = "-".join(record['name'].split("-")[:2])
 first_sample_token = record["first_sample_token"]
entries.append(( host, name, date, token, first_sample_token))
df = pd.DataFrame(entries, columns=["host", "scene_name", "date", "scene_token", "first_sample_token"] )
regressor.fit(x,y2)
 predict_2 = regressor.predict(x_test)
 predict_2 = pd.DataFrame(predict_2)
 predict_2.columns = ["Death_prediction"]
predict_2.head()<load_from_csv>
test_data_folder = '.. /input/lyft3d-mask-test-data/test_data/test_data'
class BEVImageDataset(torch.utils.data.Dataset):
def __init__(self, sample_token,test_data_folder):
self.sample_token = sample_token
self.test_data_folder = test_data_folder
def __len__(self):
return len(self.sample_token)
 def __getitem__(self, idx):
sample_token = self.sample_token[idx]
input_filepath = os.path.join(test_data_folder,f"{sample_token}_input.png")
 map_filepath = os.path.join(test_data_folder,f"{sample_token}_map.png")
 im = cv2.imread(input_filepath, cv2.IMREAD_UNCHANGED)
 map_im = cv2.imread(map_filepath, cv2.IMREAD_UNCHANGED)
 im = np.concatenate(( im, map_im), axis=2)
 im = im.astype(np.float32)/255
im = torch.from_numpy(im.transpose(2,0,1))
return im, sample_token
test_dataset = BEVImageDataset(all_sample_tokens,test_data_folder )
Samle_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv")
 Samle_submission.columns
submission = Samle_submission[["ForecastId"]]<concatenate>
class UNet(nn.Module):
def __init__(
 self,
in_channels=1,
n_classes=2,
depth=5,
wf=6,
padding=False,
batch_norm=False,
up_mode='upconv', 
):

 super(UNet, self ).__init__()
assert up_mode in('upconv', 'upsample')
 self.padding = padding
self.depth = depth
prev_channels = in_channels
self.down_path = nn.ModuleList()
for i in range(depth):
self.down_path.append(
 UNetConvBlock(prev_channels, 2 **(wf + i), padding, batch_norm)
)
 prev_channels = 2 **(wf + i)
 self.up_path = nn.ModuleList()
for i in reversed(range(depth - 1)) :
self.up_path.append(
 UNetUpBlock(prev_channels, 2 **(wf + i), up_mode, padding, batch_norm)
)
 prev_channels = 2 **(wf + i)
 self.last = nn.Conv2d(prev_channels, n_classes, kernel_size=1)
 def forward(self, x):
blocks = []
for i, down in enumerate(self.down_path):
x = down(x)
 if i != len(self.down_path)- 1:
blocks.append(x)
 x = F.max_pool2d(x, 2)
 for i, up in enumerate(self.up_path):
x = up(x, blocks[-i - 1])
 return self.last(x)
 class UNetConvBlock(nn.Module):
def __init__(self, in_size, out_size, padding, batch_norm):
super(UNetConvBlock, self ).__init__()
block = []
block.append(nn.Conv2d(in_size, out_size, kernel_size=3, padding=int(padding)))
 block.append(nn.ReLU())
 if batch_norm:
block.append(nn.BatchNorm2d(out_size))
block.append(nn.Conv2d(out_size, out_size, kernel_size=3, padding=int(padding)))
 block.append(nn.ReLU())
 if batch_norm:
block.append(nn.BatchNorm2d(out_size))
self.block = nn.Sequential(*block)
 def forward(self, x):
out = self.block(x)
 return out
class UNetUpBlock(nn.Module):
def __init__(self, in_size, out_size, up_mode, padding, batch_norm):
super(UNetUpBlock, self ).__init__()
if up_mode == 'upconv':
self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
 elif up_mode == 'upsample':
self.up = nn.Sequential(
 nn.Upsample(mode='bilinear', scale_factor=2),
nn.Conv2d(in_size, out_size, kernel_size=1), 
)
 self.conv_block = UNetConvBlock(in_size, out_size, padding, batch_norm)
 def center_crop(self, layer, target_size):
_, _, layer_height, layer_width = layer.size()
diff_y =(layer_height - target_size[0])// 2
diff_x =(layer_width - target_size[1])// 2
return layer[
:, :, diff_y :(diff_y + target_size[0]), diff_x :(diff_x + target_size[1])
 ]
def forward(self, x, bridge):
up = self.up(x)
 crop1 = self.center_crop(bridge, up.shape[2:])
 out = torch.cat([up, crop1], 1)
 out = self.conv_block(out)
 return out
Final_submission = pd.concat([predict_1,predict_2,submission],axis=1)
 Final_submission.head()<data_type_conversions>
def get_unet_model(in_channels=6, num_output_classes=2):
model = UNet(in_channels=in_channels, n_classes=num_output_classes, wf=5, depth=4, padding=True, up_mode='upsample')
 model = nn.DataParallel(model)
 return model
Final_submission.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
Final_submission = Final_submission[['ForecastId','ConfirmedCases', 'Fatalities']]
Final_submission["ConfirmedCases"] = Final_submission["ConfirmedCases"].astype(int)
 Final_submission["Fatalities"] = Final_submission["Fatalities"].astype(int )<save_to_csv>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 class_weights = torch.from_numpy(np.array([0.2] + [1.0]*len(classes), dtype=np.float32))
class_weights = class_weights.to(device )
Final_submission.to_csv("submission.csv",index=False)
 print('Model ready for submission!' )<train_model>
batch_size = 16
model10 = get_unet_model(num_output_classes=len(classes)+1)
 state = torch.load('.. /input/lyft3d-mask-test-data/unet_checkpoint_epoch_10.pth')
 model10.load_state_dict(state)
 model10 = model10.to(device)
 model10.eval() ;
model9 = get_unet_model(num_output_classes=len(classes)+1)
 state = torch.load('.. /input/lyft3d-mask-test-data/unet_checkpoint_epoch_9.pth')
 model9.load_state_dict(state)
 model9 = model9.to(device)
 model9.eval() ;
model8 = get_unet_model(num_output_classes=len(classes)+1)
 state = torch.load('.. /input/lyft3d-mask-test-data/unet_checkpoint_epoch_8.pth')
 model8.load_state_dict(state)
 model8 = model8.to(device)
 model8.eval() ;
m1 = RandomForestRegressor(n_estimators=100,max_depth=20)
 m1.fit(x,y1 )<predict_on_test>
class Model:
def __init__(self, models):
self.models = models
def __call__(self, x):
res = []
x = x.cuda()
with torch.no_grad() :
for m in self.models:
res.append(m(x))
res = torch.stack(res)
 return torch.mean(res, dim=0)
 model = Model([model8,model9,model10] )
pred1=m1.predict(x_test )<train_model>
gc.collect()
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size, shuffle=False, num_workers=os.cpu_count() *2)
 progress_bar = tqdm_notebook(test_loader)
 sample_tokens = []
all_losses = []
detection_boxes = []
detection_scores = []
detection_classes = []
background_threshold = 200
with torch.no_grad() :
for ii,(X, batch_sample_tokens)in enumerate(progress_bar):
sample_tokens.extend(batch_sample_tokens)
 X = X.to(device)
 prediction = model(X)
 prediction = F.softmax(prediction, dim=1)
 prediction_cpu = prediction.cpu().numpy()
predictions = np.round(prediction_cpu*255 ).astype(np.uint8)
 predictions_non_class0 = 255 - predictions[:,0]
predictions_opened = np.zeros(( predictions_non_class0.shape), dtype=np.uint8)
 for i, p in enumerate(predictions_non_class0):
thresholded_p =(p > background_threshold ).astype(np.uint8)
 predictions_opened[i] = cv2.morphologyEx(thresholded_p, cv2.MORPH_OPEN, kernel)
 sample_boxes,sample_detection_scores,sample_detection_classes = calc_detection_box(predictions_opened[i],
predictions[i])
 detection_boxes.append(np.array(sample_boxes))
detection_scores.append(sample_detection_scores)
 detection_classes.append(sample_detection_classes)
m1.fit(x,y2 )<predict_on_test>
def create_transformation_matrix_to_voxel_space(shape, voxel_size, offset):

 shape, voxel_size, offset = np.array(shape), np.array(voxel_size), np.array(offset)
 tm = np.eye(4, dtype=np.float32)
 translation = shape/2 + offset/voxel_size
tm = tm * np.array(np.hstack(( 1/voxel_size, [1])))
 tm[:3, 3] = np.transpose(translation)
 return tm
def transform_points(points, transf_matrix):

 if points.shape[0] not in [3,4]:
raise Exception("Points input should be(3,N)or(4,N)shape, received {}".format(points.shape))
return transf_matrix.dot(np.vstack(( points[:3, :], np.ones(points.shape[1])))) [:3, :]
def car_to_voxel_coords(points, shape, voxel_size, z_offset=0):
if len(shape)!= 3:
raise Exception("Voxel volume shape should be 3 dimensions(x,y,z)")
 if len(points.shape)!= 2 or points.shape[0] not in [3, 4]:
raise Exception("Input points should be(3,N)or(4,N)in shape, found {}".format(points.shape))
tm = create_transformation_matrix_to_voxel_space(shape, voxel_size,(0, 0, z_offset))
p = transform_points(points, tm)
 return p
def create_voxel_pointcloud(points, shape, voxel_size=(0.5,0.5,1), z_offset=0):
points_voxel_coords = car_to_voxel_coords(points.copy() , shape, voxel_size, z_offset)
 points_voxel_coords = points_voxel_coords[:3].transpose(1,0)
 points_voxel_coords = np.int0(points_voxel_coords)
 bev = np.zeros(shape, dtype=np.float32)
 bev_shape = np.array(shape)
 within_bounds =(np.all(points_voxel_coords >= 0, axis=1)* np.all(points_voxel_coords < bev_shape, axis=1))
points_voxel_coords = points_voxel_coords[within_bounds]
coord, count = np.unique(points_voxel_coords, axis=0, return_counts=True)
 bev[coord[:,1], coord[:,0], coord[:,2]] = count
return bev
def normalize_voxel_intensities(bev, max_intensity=16):
return(bev/max_intensity ).clip(0,1)
pred2=m1.predict(x_test)
 pred2<create_dataframe>
sub = pd.DataFrame(list(sub.items()))
sub.columns = sample_sub.columns
sub.head()
<create_dataframe><EOS> 
sub.to_csv('lyft3d_pred.csv',index=False )