def __init__(self, annotation_file_path, train_image_folder_path): self.annotation_file = annotation_file_path self.PATH = train_image_folder_path train_captions, img_path_vector = deal_annotations( self.annotation_file, self.PATH) image_preprocess(img_path_vector) caption_vector, self.max_length, self.tokenizer = text_preprocess( train_captions, vocab_size=settings.vocab_size) dataset = build_dataset(img_path_vector, caption_vector) self.dataset = dataset self.total_num = len(img_path_vector)
def build_dataset(filenames, nfeatures): # allocate an array for images num_images = len(filenames) raw_images = numpy.empty(num_images, dtype=object) bin_images = numpy.empty(num_images, dtype=object) # Read raw and binary images into these preallocated numpy arrays of objects index = 0 pixel_cnt = 0 for f in filenames: print("%d: [%s]" % (index, f)) rawpath = config["rawdir"] + "/" + f binpath = config["bindir"] + "/" + f raw_image = Image.open(rawpath) raw_images[index] = numpy.array(raw_image) raw_image.close() bin_image = Image.open(binpath) bin_images[index] = numpy.array(bin_image) bin_images[index] = cv2.normalize(bin_images[index], None, 0, 1, cv2.NORM_MINMAX, cv2.CV_8U) # SHENEMAN bin_image.close() pixel_cnt += raw_images[index].size index += 1 print("Number of Pixels in %d images: %d" % (index, pixel_cnt)) # Now that we know the number of pixels, we can allocate raw and bin arrays raw = numpy.empty((pixel_cnt, nfeatures), dtype=numpy.uint8) bin = numpy.empty(pixel_cnt, dtype=numpy.uint8) # # Process raw images # pixel_cnt = 0 pixel_index = 0 for raw_cv2 in raw_images: pixels = preprocess.image_preprocess(f, nfeatures, raw_cv2) raw[pixel_index:pixel_index + pixels.shape[0], :] = pixels pixel_index += pixels.shape[0] pixel_cnt += raw_cv2.size # # Process binary images # pixel_index = 0 for bin_cv2 in bin_images: pixels = bin_cv2.flatten(order='F') bin[pixel_index:pixel_index + len(pixels)] = pixels pixel_index += len(pixels) return (raw, bin, pixel_cnt)
def predict(model): predicted02 = [] predicted03 = [] predicted04 = [] predicted05 = [] for name in submit['Id']: image = image_preprocess('../input/test/', name) image = cv2.resize(image, input_shape) image = np.reshape(image, [1, input_shape[0], input_shape[1], n_channels]) prediction = model.predict(image)[0] indices02 = np.argwhere(prediction >= 0.2).flatten() labels02 = ' '.join(str(l) for l in indices02) indices03 = np.argwhere(prediction >= 0.3).flatten() labels03 = ' '.join(str(l) for l in indices03) indices04 = np.argwhere(prediction >= 0.4).flatten() labels04 = ' '.join(str(l) for l in indices04) indices05 = np.argwhere(prediction >= 0.5).flatten() labels05 = ' '.join(str(l) for l in indices05) predicted02.append(labels02) predicted03.append(labels03) predicted04.append(labels04) predicted05.append(labels05) return predicted02, predicted03, predicted04, predicted05
def __data_generation(self, batch_samples): 'Generate data' images = [] labels = [] for batch_sample in batch_samples: image = image_preprocess(self.input_dir, batch_sample) if image.shape != self.image_shape: image = cv2.resize(image, self.image_shape) label = self.label_dict[batch_sample] if self.augment: images.extend([image, self.do_augmentation(image)]) labels.extend([label, label]) else: images.append(image) labels.append(label) X_train = np.array(images) y_train = np.array(labels) return sklearn.utils.shuffle(X_train, y_train)
output = config["outputdir"] + "/" + filename ###################################################################### # Load all of the files, extract all features. This builds "dataset" # which is a list of feature arrays. Every element in the list is # a list of preprocessed images # rawpath = config["rawdir"] + "/" + filename print("Loading: %s" % rawpath) raw_img = Image.open(rawpath) numcols, numrows = raw_img.size raw_cv2 = numpy.array(raw_img) raw_img.close() data = preprocess.image_preprocess(filename, nfeatures, raw_cv2) print("Image Size: numcols=%d x numrows=%d" % (numcols, numrows)) print("Num Features: %d" % nfeatures) # classify our input pixels RF_Y_pred = rf_classifier.predict(data) # Random Forest MLP_Y_pred = mlp_classifier.predict(data) # Neural Network XGB_Y_pred = xgb_classifier.predict(data) # XGBoost Classifier # Majority votes for lipid pixels across ML methods pred = ((RF_Y_pred.astype(numpy.uint16) + MLP_Y_pred.astype(numpy.uint16) + XGB_Y_pred.astype(numpy.uint16)) / 255).astype(numpy.uint8) print("Lipid Pixels with only ONE vote: %d" % (numpy.sum(pred == 1))) print("Lipid Pixels with TWO votes: %d" % (numpy.sum(pred == 2)))
main_path = "D:/Projects/theschoolofai/repo/tsai/S14/" main_path = "C:/Users/pbhat/Google Drive/S14/" bg_image_path = main_path + "background/" fg_image_path = main_path + "foreground/" bg_img_size = 128 fg_img_size = 32 bg_images = os.listdir(bg_image_path) fg_images = os.listdir(fg_image_path) start = datetime.datetime.now() for bg_img in bg_images: background_image = image_preprocess(path=bg_image_path + bg_img, size=(bg_img_size, bg_img_size), channels=3) for fg_img in fg_images: # print(fg_img) for flip in [True, False]: txt = "train_{0}_{1}_{2}_".format( bg_img.split(".")[0], fg_img.split(".")[0], str(int(flip))) foreground_image = image_preprocess(path=fg_image_path + fg_img, size=(fg_img_size, fg_img_size), channels=4, flip=flip) foreground_image_mask = create_forground_mask(foreground_image) for pos in range(20): x = randint(0, (bg_img_size - fg_img_size))