def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Gather Image IDS ######################################################################## image_ids = gather_image_ids(test_images_path) ######################################################################## # Generate Predictions ######################################################################## evaluate(AICROWD_TEST_IMAGES_PATH, AICROWD_PREDICTIONS_OUTPUT_PATH) ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success( {"predictions_output_path": predictions_output_path})
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Load Tests Meta Data file # and iterate over all its rows # # Each Row contains the following information : # # - hashed_id : a unique id for each test image # - filename : filename of the image # - country : Country where this image was taken # - continent : Continent where this image was taken ######################################################################## OUTPUT_LINES = [] HEADER = ['hashed_id'] + VALID_SNAKE_SPECIES OUTPUT_LINES.append(",".join(HEADER)) tests_df = pd.read_csv(AICROWD_TEST_METADATA_PATH) for _idx, row in tests_df.iterrows(): image_id = row["hashed_id"] country = row["country"] continent = row["continent"] filename = row["filename"] filepath = os.path.join(AICROWD_TEST_IMAGES_PATH, filename) predictions = get_random_prediction(image_id) PREDICTION_LINE = [image_id] + [str(x) for x in predictions.tolist()] OUTPUT_LINES.append(",".join(PREDICTION_LINE)) ######################################################################## # Register Prediction # # Note, this prediction register is not a requirement. It is used to # provide you feedback of how far are you in the overall evaluation. # In the absence of it, the evaluation will still work, but you # will see progress of the evaluation as 0 until it is complete # # Here you simply announce that you completed processing a set of # image_ids ######################################################################## aicrowd_helpers.execution_progress({ "image_ids": [image_id] ### NOTE : This is an array of image_ids }) # Write output fp = open(AICROWD_PREDICTIONS_OUTPUT_PATH, "w") fp.write("\n".join(OUTPUT_LINES)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success( {"predictions_output_path": AICROWD_PREDICTIONS_OUTPUT_PATH})
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Generate Predictions ######################################################################## cfg = get_cfg() cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.WEIGHTS = model_path cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = threshold # set the testing threshold for this model cfg.MODEL.ROI_HEADS.NUM_CLASSES = 273 #cfg.MODEL.DEVICE = "cpu" predictor = DefaultPredictor(cfg) results = [] del cfg for i in os.listdir(test_images_path): img_path =test_images_path + "/" +str(i) im = cv2.imread(img_path) outputs = predictor(im) instances = outputs["instances"].to(cpu_device) fname = int(i.split('.')[0]) result = instances_to_coco_json(instances,fname) if(len(result)!=0): for ele in result: matchId = ele['category_id'] ele['category_id'] = reverse_id_mapping[str(matchId)] results.append(ele) aicrowd_helpers.execution_progress({ "image_ids" : [fname] }) # Write output fp = open(predictions_output_path, "w") fp.write(json.dumps(results)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success({ "predictions_output_path" : predictions_output_path })
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Gather Image IDS ######################################################################## image_ids = gather_image_ids(test_images_path) ######################################################################## # Generate Predictions ######################################################################## predictions = [] print("Number of images found:", len(image_ids)) for image_id in image_ids: number_of_annotations = np.random.randint(0, MAX_NUMBER_OF_ANNOTATIONS) for _idx in range(number_of_annotations): print(image_id) _annotation = single_annotation(image_id) predictions.append(_annotation) ######################################################################## # Register Prediction # # Note, this prediction register is not a requirement. It is used to # provide you feedback of how far are you in the overall evaluation. # In the absence of it, the evaluation will still work, but you # will see progress of the evaluation as 0 until it is complete # # Here you simply announce that you completed processing a set of # image-ids ######################################################################## aicrowd_helpers.execution_progress({"image_ids": [image_id]}) # Write output fp = open(predictions_output_path, "w") fp.write(json.dumps(predictions)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success( {"predictions_output_path": predictions_output_path})
def run(): aicrowd_helpers.execution_start() #MAGIC HAPPENS BELOW torch.backends.cudnn.benchmark = True device = torch.device("cuda:0") assert torch.cuda.is_available() precrop, crop = bit_hyperrule.get_resolution_from_dataset( 'snakes_dataset') # verify valid_tx = tv.transforms.Compose([ tv.transforms.Resize((crop, crop)), tv.transforms.ToTensor(), tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) given_df = pd.read_csv(AICROWD_TEST_METADATA_PATH) valid_set = SnakeDataset(AICROWD_TEST_IMAGES_PATH, is_train=False, transform=valid_tx, target_transform=None, csv_file=AICROWD_TEST_METADATA_PATH) # verify valid_loader = torch.utils.data.DataLoader(valid_set, batch_size=32, shuffle=False, num_workers=0, pin_memory=True, drop_last=False) model = models.KNOWN_MODELS['BiT-M-R50x1']( head_size=len(VALID_SNAKE_SPECIES), zero_head=True) model = torch.nn.DataParallel(model) optim = torch.optim.SGD(model.parameters(), lr=0.003, momentum=0.9) model_loc = pjoin('models', 'initial.pth.tar') checkpoint = torch.load(model_loc, map_location='cpu') model.load_state_dict(checkpoint['model']) model = model.to(device) model.eval() results = np.empty((0, 783), float) for b, (x, y) in enumerate( valid_loader): #add name to dataset, y must be some random label with torch.no_grad(): x = x.to(device, non_blocking=True) y = y.to(device, non_blocking=True) logits = model(x) softmax_op = torch.nn.Softmax(dim=1) probs = softmax_op(logits) data_to_save = probs.data.cpu().numpy() results = np.concatenate((results, data_to_save), axis=0) filenames = given_df['hashed_id'].tolist() country_prob = pd.read_csv( pjoin('metadata', 'probability_of_species_per_country.csv')) country_name = country_prob[['Species/Country']] country_dict = {name[0]: i for i, name in enumerate(country_name.values)} given_country = given_df[['country']] country_list = [] for country in given_country.values: country_list.append(str(country[0]).lower().replace( ' ', '-')) # has to be a better way adjusted_results = [] for i, result in enumerate(results): probs = result assert len(prob) == 783 try: country_now = country_list[i] country_location = country_dict[country_now] country_prob_per_this_country = country_prob.loc[[ country_location ]].values[0][1:] adjusted = country_prob_per_this_country * probs adjusted_results.append(adjusted) # verify, we need list of list except: adjusted_results.append(probs) assert len(adjusted_results) == len(results) #normalize normalized_results = adjusted_results / adjusted_results.sum(axis=1)[:, None] df = pd.DataFrame(data=normalized_results, index=filenames, columns=VALID_SNAKE_SPECIES) df.index.name = 'hashed_id' pd.to_csv(AICROWD_PREDICTIONS_OUTPUT_PATH, index=True) aicrowd_helpers.execution_success( {"predictions_output_path": AICROWD_PREDICTIONS_OUTPUT_PATH})
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Gather Image Names ######################################################################## image_names = gather_image_names(test_images_path) ######################################################################## # Do your magic here to train the model ######################################################################## # Preprocess first here src = (ImageList.from_folder( path='fastai-data').split_by_rand_pct(0.0).label_from_folder() ) # any folder which has sub-folders will work tfms = get_transforms(do_flip=True, flip_vert=False, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75) # some tfms data = (src.transform(tfms, size=360, resize_method=ResizeMethod.SQUISH).databunch( bs=32, num_workers=0).normalize(imagenet_stats)) learn = Learner( data, SnakeDetector(arch=models.resnet50), loss_func=L1Loss( )) #temp data, we wont be using this, we will be using src_new learn.split([learn.model.cnn[:6], learn.model.cnn[6:], learn.model.head]) state_dict = torch.load( 'fastai-data/models/snake-detection-model.pth') #our trained model learn.model.load_state_dict(state_dict['model']) if not os.path.exists('preprocessed-images'): os.makedirs('preprocessed-images') #directory to store files src_new = (ImageList.from_folder( path=test_images_path).split_by_rand_pct(0.0).label_from_folder() ) # fetch given test images from data/images for filename in src_new.items: try: im = cv2.imread(f"{filename}", cv2.IMREAD_COLOR) im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) im = cv2.resize(im, (360, 360), interpolation=cv2.INTER_AREA) im_height, im_width, _ = im.shape orig_im = cv2.imread(f"{filename}", cv2.IMREAD_COLOR) orig_im_height, orig_im_width, _ = orig_im.shape to_pred = open_image(filename) _, _, bbox = learn.predict(to_pred) im_original = cv2.imread(f"{filename}", cv2.IMREAD_COLOR) im_original = cv2.cvtColor(im_original, cv2.COLOR_BGR2RGB) im_original.shape im_original_width = im_original.shape[1] im_original_height = im_original.shape[0] bbox_new = bbox bbox_new[0] = bbox_new[0] * im_original_width bbox_new[2] = bbox_new[2] * im_original_width bbox_new[1] = bbox_new[1] * im_original_height bbox_new[3] = bbox_new[3] * im_original_height x_min, y_min, x_max, y_max = map(int, bbox_new) #cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness) im_original = im_original[ y_min:y_max, x_min: x_max] #cropping is just slicing opencv uses h,w . which is y,x here im_original = cv2.cvtColor(im_original, cv2.COLOR_BGR2RGB) filename_str = str(filename) to_save = filename_str.replace( test_images_path, 'preprocessed-images' ) # original file is in /data/images/*.jpg -> store it at preprocessed-images/*.jpg and use preprocessed-images folder later cv2.imwrite(to_save, im_original) except: pass del learn gc.collect() model_name = 'efficientnet-b5' image_size = EfficientNet.get_image_size(model_name) model = EfficientNet.from_pretrained(model_name) np.random.seed(13) src = (ImageList.from_folder( path='fastai-data').split_by_rand_pct(0.2).label_from_folder()) object_detection_results_path = os.getcwd( ) + "/preprocessed-images" #output of previous step is stored here. src.add_test_folder(object_detection_results_path) tfms = ([rotate(degrees=(-90, 90), p=0.8)], []) bs = 8 data = (src.transform(tfms, size=image_size, resize_method=ResizeMethod.SQUISH).databunch( bs=bs, num_workers=0).normalize(imagenet_stats)) model.add_module('_fc', nn.Linear( 2048, data.c)) #Replace the final layer of b5 model with number of classes loss_func = LabelSmoothingCrossEntropy() #following EfficientNet paper RMSprop = partial(torch.optim.RMSprop) #Following EfficientNet paper learn = Learner(data, model, loss_func=loss_func, opt_func=RMSprop, metrics=[accuracy, FBeta(beta=1, average='macro')]) learn.split( [[learn.model._conv_stem, learn.model._bn0, learn.model._blocks[:19]], [learn.model._blocks[19:], learn.model._conv_head], [learn.model._bn1, learn.model._fc]]) #for differential learning learn.load( 'b5-seed-13-round-3' ) #best single model - 85.2 local cv. Try ensemble later. with 83.1 and 85.2 models ######################################################################## # Generate Predictions ######################################################################## preds, _ = learn.TTA(ds_type=DatasetType.Test) # Test time augmentation probs_seed_13 = np.exp(preds) / np.exp(preds).sum(1)[:, None] del learn gc.collect() #garbage collect model = EfficientNet.from_pretrained(model_name) model.add_module('_fc', nn.Linear(2048, data.c)) learn = Learner(data, model, loss_func=loss_func, opt_func=RMSprop, metrics=[accuracy, FBeta(beta=1, average='macro')]) #mew learner learn.split( [[learn.model._conv_stem, learn.model._bn0, learn.model._blocks[:19]], [learn.model._blocks[19:], learn.model._conv_head], [learn.model._bn1, learn.model._fc]]) #not needed, but not takin chances learn.load('b5-seed-15-round-7') #83.6, 83.1 localcv model preds, _ = learn.TTA(ds_type=DatasetType.Test) probs_seed_15 = np.exp(preds) / np.exp(preds).sum(1)[:, None] probs = (probs_seed_13 + probs_seed_15) / 2 probs_np = probs.numpy() df_test = pd.read_csv( 'given_submission_sample_file.csv', low_memory=False ) #use the given sample file to replace probabilites with our model predictions, This way, no need to worry about corrupted images df_classes = pd.read_csv('class.csv', low_memory=False) #class mapping data_dict = df_classes.set_index( 'class_idx')['original_class'].to_dict() #for look up probs_np = probs.numpy() df_testfile_mapping = pd.DataFrame(columns=['filename', 'map']) df_testfile_mapping['filename'] = df_test['filename'] for i, row in df_testfile_mapping.iterrows(): row['map'] = i data_dict_filename = df_testfile_mapping.set_index( 'filename' )['map'].to_dict( ) # for lookup, returns the index where the filename is found in the given submission file i = 0 for test_file in data.test_ds.items: filename = (os.path.basename(test_file)) map_val = int(data_dict_filename[filename]) for c in range(0, 45): df_test.loc[ map_val, data_dict[int(data.classes[c].split("-")[1])]] = probs_np[i][c] i += 1 #around 7 predictions causes assertion error, for now submit them as class-204 for i, row in df_test.iterrows(): sum_temp = row[1:46].sum() low_limit = 1 - 1e-6 high_limit = 1 + 1e-6 if not (sum_temp >= low_limit and sum_temp <= high_limit): for c in range(1, 46): df_test.iloc[i, c] = 0. df_test.loc[i, 'thamnophis_sirtalis'] = 1. df_test.to_csv('generated-submission-3.csv', index=False) copyfile('generated-submission-3.csv', predictions_output_path) #save to output path ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success( {"predictions_output_path": predictions_output_path})
def run(): ######################################################################## # Register Prediction Start ######################################################################## import os aicrowd_helpers.execution_start() os.environ['AICROWD_TEST_IMAGES_PATH'] = 'data/round1' os.environ['AICROWD_PREDICTIONS_OUTPUT_PATH'] = 'random_prediction.csv' ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Gather Image Names ######################################################################## image_names = gather_image_names(test_images_path) ######################################################################## # Do your magic here to train the model ######################################################################## import numpy as np import os import glob import keras from keras.models import load_model import efficientnet.keras as efn from efficientnet.keras import EfficientNetB5 model = load_model('model-224-123-16-1.178489-1.705530.hdf5') counter2 = 0 df_data = [] df_count = [] lim = 100000 lim2 = 100000 IMAGE_folder = '/home/amaury2_pichat/data/snake/train' for folder in os.listdir(IMAGE_folder): counter = 0 counter2 = 0 if len(df_data) < lim: for file in os.listdir(IMAGE_folder + '/' + folder): if counter2 < lim2: statinfo = os.stat(IMAGE_folder + '/' + folder + '/' + file) if statinfo.st_size != 0 and counter2 < lim2: df_data.append((folder, file)) counter += 1 counter2 += 1 import pandas as pd df_data = pd.DataFrame(df_data, columns=['Folder', 'id']) # df_data['Folder']='' + df_data['Folder'] df_data['Classes'] = df_data['Folder'] df_data['Class'] = df_data['Classes'] df_data[ 'Path'] = IMAGE_folder + '/' + df_data['Folder'] + '/' + df_data['id'] df_data.drop(['Classes', 'Folder'], axis=1, inplace=True) from keras.preprocessing.image import ImageDataGenerator IMAGE_SIZE = 224 valAug = ImageDataGenerator(rescale=1 / 255.0) pred_gen = valAug.flow_from_dataframe(df_data, x_col="Path", y_col="Class", shuffle=False, target_size=(IMAGE_SIZE, IMAGE_SIZE), batch_size=1, class_mode='categorical') LINES = [] with open('data/class_idx_mapping.csv') as f: classes = ['filename'] for line in f.readlines()[1:]: class_name = line.split(",")[0] classes.append(class_name) LINES.append(','.join(classes)) predictions = model.predict_generator(pred_gen, verbose=1, steps=len(pred_gen.filenames)) compteur = 0 for _file_path in pred_gen.filenames: file_and_pred = _file_path[_file_path.rfind('\\') + 2:] LINES.append(_file_path[_file_path.rfind('\\') + 2:] + "," + ",".join(str(x) for x in predictions[compteur])) compteur += 1 #images_path = AICROWD_TEST_IMAGES_PATH + '/*.jpg' #for _file_path in glob.glob(images_path): # probs = softmax(np.random.rand(45)) # probs = list(map(str, probs)) # LINES.append(",".join([os.path.basename(_file_path)] + probs)) AICROWD_PREDICTIONS_OUTPUT_PATH = os.environ[ 'AICROWD_PREDICTIONS_OUTPUT_PATH'] fp = open(AICROWD_PREDICTIONS_OUTPUT_PATH, "w") fp.write("\n".join(LINES)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success( {"predictions_output_path": predictions_output_path})
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Gather Input and Output paths from environment variables ######################################################################## test_images_path, predictions_output_path = gather_input_output_path() ######################################################################## # Gather Image Names ######################################################################## image_names = gather_image_names(test_images_path) ######################################################################## # Do your magic here to train the model ######################################################################## classes = get_snake_classes() def softmax(x): """Compute softmax values for each sets of scores in x.""" e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=0) ######################################################################## # Generate Predictions ######################################################################## LINES = [] LINES.append(','.join(['filename'] + classes)) predictions = [] for image_name in image_names: probs = softmax(np.random.rand(45)) probs = list(map(str, probs)) LINES.append(",".join([image_name] + probs)) ######################################################################## # Register Prediction # # Note, this prediction register is not a requirement. It is used to # provide you feedback of how far are you in the overall evaluation. # In the absence of it, the evaluation will still work, but you # will see progress of the evaluation as 0 until it is complete # # Here you simply announce that you completed processing a set of # image_names ######################################################################## aicrowd_helpers.execution_progress({ "image_names" : [image_name] }) # Write output fp = open(predictions_output_path, "w") fp.write("\n".join(LINES)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success({ "predictions_output_path" : predictions_output_path })
def run(): ######################################################################## # Register Prediction Start ######################################################################## aicrowd_helpers.execution_start() ######################################################################## # Load Tests Meta Data file # and iterate over all its rows # # Each Row contains the following information : # # - `index` : Unique ID of each record # - `intclinicaltrialid` : description to be added # - `intdesignkeywordid` : description to be added # - `strdesignkeyword` : description to be added # - .... and so on ######################################################################## OUTPUT_LINES = [] HEADER = ['index'] + VALID_CLASSES OUTPUT_LINES.append(",".join(HEADER)) tests_df = pd.read_csv(AICROWD_TEST_DATA_PATH, index_col=0) for index_id, row in tqdm.tqdm(tests_df.iterrows()): index_id = str(index_id) intclinicaltrialid = row['intclinicaltrialid'] intdesignkeywordid = row['intdesignkeywordid'] ## ## ... and so on ## TODO : Provide example re predictions = get_random_prediction(index_id) PREDICTION_LINE = [index_id] + [str(x) for x in predictions.tolist()] OUTPUT_LINES.append(",".join(PREDICTION_LINE)) ######################################################################## # Register Prediction # # Note, this prediction register is not a requirement. It is used to # provide you feedback of how far are you in the overall evaluation. # In the absence of it, the evaluation will still work, but you # will see progress of the evaluation as 0 until it is complete # # Here you simply announce that you completed processing a set of # index_ids ######################################################################## aicrowd_helpers.execution_progress({ "index_ids" : [index_id] ### NOTE : This is an array of index_ids }) # Write output fp = open(AICROWD_PREDICTIONS_OUTPUT_PATH, "w") fp.write("\n".join(OUTPUT_LINES)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success({ "predictions_output_path" : AICROWD_PREDICTIONS_OUTPUT_PATH })
# In the absence of it, the evaluation will still work, but you # will see progress of the evaluation as 0 until it is complete # # Here you simply announce that you completed processing a set of # image_names ######################################################################## aicrowd_helpers.execution_progress({ "image_names" : [image_name] }) # Write output fp = open(predictions_output_path, "w") fp.write("\n".join(LINES)) fp.close() ######################################################################## # Register Prediction Complete ######################################################################## aicrowd_helpers.execution_success({ "predictions_output_path" : predictions_output_path }) if __name__ == "__main__": try: run() except Exception as e: error = traceback.format_exc() print(error) aicrowd_helpers.execution_error(error)