Kaggle competition: Lyft 3D Object Detection for Autonomous Vehicles
<SOS> metric: Lyft3DObjectDetectionAP Kaggle data source: 3d-object-detection-for-autonomous-vehicles<load_from_csv>
!pip install lyft-dataset-sdk -q
Sample_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv")
 Sample_submission.columns
submission = Sample_submission[["ForecastId"]]<concatenate>
os.environ["OMP_NUM_THREADS"] = "1"
%matplotlib inline
RF_submission = pd.concat([pred1,pred2,submission],axis=1)
 RF_submission.head() 
<save_to_csv>
class LyftTestDataset(LyftDataset):

 def __init__(self, data_path: str, json_path: str, verbose: bool = True, map_resolution: float = 0.1):

 self.data_path = Path(data_path ).expanduser().absolute()
self.json_path = Path(json_path)
 self.table_names = [
"category",
"attribute",
"sensor",
"calibrated_sensor",
"ego_pose",
"log",
"scene",
"sample",
"sample_data",
"map",
]
start_time = time.time()
self.category = self.__load_table__("category")
 self.attribute = self.__load_table__("attribute")
 self.sensor = self.__load_table__("sensor")
 self.calibrated_sensor = self.__load_table__("calibrated_sensor")
 self.ego_pose = self.__load_table__("ego_pose")
 self.log = self.__load_table__("log")
 self.scene = self.__load_table__("scene")
 self.sample = self.__load_table__("sample")
 self.sample_data = self.__load_table__("sample_data")
 self.map = self.__load_table__("map")
 if verbose:
for table in self.table_names:
print("{} {},".format(len(getattr(self, table)) , table))
print("Done loading in {:.1f} seconds.
 ======".format(time.time() - start_time))
self.explorer = LyftDatasetExplorer(self)
 self.__make_reverse_index__(verbose)
 def __make_reverse_index__(self, verbose: bool)-> None:

 start_time = time.time()
if verbose:
print("Reverse indexing...")
 self._token2ind = dict()
for table in self.table_names:
self._token2ind[table] = dict()
for ind, member in enumerate(getattr(self, table)) :
self._token2ind[table][member["token"]] = ind
for record in self.sample_data:
cs_record = self.get("calibrated_sensor", record["calibrated_sensor_token"])
 sensor_record = self.get("sensor", cs_record["sensor_token"])
 record["sensor_modality"] = sensor_record["modality"]
record["channel"] = sensor_record["channel"]
for record in self.sample:
record["data"] = {}
record["anns"] = []
for record in self.sample_data:
if record["is_key_frame"]:
sample_record = self.get("sample", record["sample_token"])
 sample_record["data"][record["channel"]] = record["token"]
if verbose:
print("Done reverse indexing in {:.1f} seconds.
 ======".format(time.time() - start_time))
RF_submission.to_csv("submission.csv",index=False)
 print('Model ready for submission!' )<import_modules>
!tar -xf.. /input/lyft3d-test-dataset/lyft3d_bev_test_data.tar.gz
import keras<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 )
model = Sequential()
model.add(Dense(100, activation='relu', input_dim=5))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
<train_model>
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 = LyftTestDataset(data_path='.', json_path='.. /input/3d-object-detection-for-autonomous-vehicles/test_data', verbose=True )
model.fit(x, y1, epochs=20, verbose=0)
 yhat = model.predict(x_test, verbose=0)
 print(yhat )<train_model>
voxel_size =(0.4, 0.4, 1.5)
 z_offset = -2.0
bev_shape =(336, 336, 3)
 box_scale = 0.8
model.fit(x, y2, epochs=20, verbose=0)
 yhat1 = model.predict(x_test, verbose=0)
 print(yhat1 )<load_from_csv>
class BEVImageDataset(torch.utils.data.Dataset):
def __init__(self, input_filepaths, map_filepaths=None):
self.input_filepaths = input_filepaths
def __len__(self):
return len(self.input_filepaths)
 def __getitem__(self, idx):
input_filepath = self.input_filepaths[idx]
sample_token = input_filepath.split("/")[-1].replace("_input.png","")
 im = cv2.imread(input_filepath, cv2.IMREAD_UNCHANGED)
 im = im.astype(np.float32)/255
im = torch.from_numpy(im.transpose(2,0,1))
return im, sample_token
test_data_folder = './artifacts/'
input_filepaths = sorted(glob.glob(os.path.join(test_data_folder, "*_input.png")))
 test_dataset = BEVImageDataset(input_filepaths)
 im, sample_token = test_dataset[1]
im = im.numpy()
plt.figure(figsize=(16,8))
plt.imshow(im.transpose(1,2,0)[...,:3])
 plt.title(sample_token)
 plt.show()
visualize_lidar_of_sample(sample_token )
Sample_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv")
 Sample_submission.columns
submission_mlp = Sample_submission[["ForecastId"]]<prepare_output>
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
cases=pd.DataFrame(yhat)
 cases.columns=["ConfirmedCases"]
fatal=pd.DataFrame(yhat1)
 fatal.columns=["Fatalities"]
Final_submission = pd.concat([cases,fatal,submission_mlp],axis=1)
 Final_submission.head()<save_to_csv>
def get_unet_model(in_channels=3, 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.to_csv("submission.csv",index=False)
 print('Model ready for submission!' )<import_modules>
def visualize_predictions(input_image, prediction, n_images=2, apply_softmax=True):

 prediction = prediction[:n_images]
input_image = input_image[:n_images]
prediction = prediction.detach().cpu().numpy()
if apply_softmax:
prediction = scipy.special.softmax(prediction, axis=1)
 class_one_preds = np.hstack(1-prediction[:,0])
 class_rgb = np.repeat(class_one_preds[..., None], 3, axis=2)
 class_rgb[...,2] = 0
input_im = np.hstack(input_image.cpu().numpy().transpose(0,2,3,1))
if input_im.shape[2] == 3:
input_im_grayscale = np.repeat(input_im.mean(axis=2)[..., None], 3, axis=2)
 overlayed_im =(input_im_grayscale*0.6 + class_rgb*0.7 ).clip(0,1)
 else:
input_map = input_im[...,3:]
overlayed_im =(input_map*0.6 + class_rgb*0.7 ).clip(0,1)
 thresholded_pred = np.repeat(class_one_preds[..., None] > 0.5, 3, axis=2)
 fig = plt.figure(figsize=(12,26))
plot_im = np.vstack([class_rgb, input_im[...,:3], overlayed_im, thresholded_pred] ).clip(0,1 ).astype(np.float32)
 plt.imshow(plot_im)
 plt.axis("off")
 plt.show()
import xgboost as xgb
from xgboost import plot_importance, plot_tree
from sklearn.metrics import mean_squared_error, mean_absolute_error 
<train_model>
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 )
reg = xgb.XGBRegressor(n_estimators=1000)
 reg.fit(x, y1,
verbose=False )<train_model>
batch_size = 8
epochs = 15
model = get_unet_model(num_output_classes=len(classes)+1)
 state = torch.load('.. /input/reference-model/artifacts/unet_checkpoint_epoch_15.pth')
 model.load_state_dict(state)
 model = model.to(device)
 model.eval() ;
reg = xgb.XGBRegressor(n_estimators=1000)
 reg.fit(x, y2,
verbose=False )<predict_on_test>
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
def open_preds(predictions_non_class0):
predictions_opened = np.zeros(( predictions_non_class0.shape), dtype=np.uint8)
 for i, p in enumerate(tqdm(predictions_non_class0)) :
thresholded_p =(p > background_threshold ).astype(np.uint8)
 predictions_opened[i] = cv2.morphologyEx(thresholded_p, cv2.MORPH_OPEN, kernel)
 return predictions_opened
XG_fatalities = reg.predict(x_test )<predict_on_test>
gc.collect()
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size, shuffle=True, 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() :
model.eval()
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)
XG_Confirmed = reg.predict(x_test)
<load_from_csv>
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)
Sample_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv")
 Sample_submission.columns
submission_xg = Sample_submission[["ForecastId"]]<prepare_output>
sub = pd.DataFrame(list(sub.items()))
sub.columns = sample_sub.columns
sub.head()
<define_search_space><EOS> 
sub.to_csv('lyft3d_pred.csv',index=False )