Beispiel #1
0
def analysis(img, model, preprocess_input, decode_predictions, layer_name,
             n_classes):
    # Preprocess data
    imgArray, originalImage = process_image(img, preprocess_input)
    preds = model.predict(imgArray)
    pred_class = np.argmax(preds)  # Change here to get view of something else
    decoded_preds = decode_predictions(preds)
    class_data = decoded_preds[0][0]

    # Compute methods
    localization_grad = grad_cam(imgArray, model, pred_class, originalImage,
                                 relu_activation, linear, layer_name,
                                 n_classes)
    localization_squad = grad_cam(imgArray, model, pred_class, originalImage,
                                  relu_activation, squared_weights, layer_name,
                                  n_classes)
    bprop = guided_backprop(imgArray, model, pred_class, layer_name, n_classes)

    # Make it three dimensions to allow for multiplication
    localization_grad = np.array(
        [localization_grad, localization_grad, localization_grad])
    localization_grad = np.swapaxes(localization_grad, 0, 2)
    localization_grad = np.swapaxes(localization_grad, 0, 1)
    localization_squad = np.array(
        [localization_squad, localization_squad, localization_squad])
    localization_squad = np.swapaxes(localization_squad, 0, 2)
    localization_squad = np.swapaxes(localization_squad, 0, 1)

    # Combine gradcam and backprop to get guided gradcam
    guided_gradcam = np.multiply(localization_grad, bprop)
    guided_squadcam = np.multiply(localization_squad, bprop)
    guided_gradcam = rescale(guided_gradcam)
    guided_squadcam = rescale(guided_squadcam)

    return bprop, guided_gradcam, guided_squadcam, class_data
Beispiel #2
0
    def save(self, *args, **kwargs):
        status = kwargs.pop('status', None)
        if self.image:
            # ProfileSerializer update에서, image를 제외한 다른
            # Profile 필드들을 새로 업데이트하는 경우 resizing을 실시하지 않는다
            if status != 'same-image':
                image_file_byte_data = self.image.read()
                thumbnail_image_file_200 = rescale(image_file_byte_data,
                                                   200,
                                                   200,
                                                   force=True)
                thumbnail_image_file_50 = rescale(image_file_byte_data,
                                                  50,
                                                  50,
                                                  force=True)
                thumbnail_image_file_25 = rescale(image_file_byte_data,
                                                  25,
                                                  25,
                                                  force=True)

                self.thumbnail_image_200.save(f'{self.image.name}',
                                              thumbnail_image_file_200,
                                              save=False)
                self.thumbnail_image_50.save(f'{self.image.name}',
                                             thumbnail_image_file_50,
                                             save=False)
                self.thumbnail_image_25.save(f'{self.image.name}',
                                             thumbnail_image_file_25,
                                             save=False)

        super().save(*args, **kwargs)
Beispiel #3
0
def _parallel_eval(Classifier, params, X, y, w, n_repeat=5, verbose=1):
    if verbose > 0:
        print "[Start]", params

    thresholds, scores, decisions = [], [], []

    for i in range(n_repeat):
        if verbose > 0:
            print "Fold", i

        X_train, X_valid, y_train, y_valid, w_train, w_valid = train_test_split(X, y, w, train_size=0.5, random_state=i)
        X_train = np.asfortranarray(X_train, dtype=np.float32)

        w_train = rescale(w_train)
        w_train = rebalance(y_train, w_train)

        clf = Classifier(**params)
        try:
            clf = clf.fit(X_train, y_train, sample_weight=w_train)
        except:
            clf = clf.fit(X_train, y_train)

        threshold, score, d = find_threshold(clf, X_valid, y_valid, w_valid)
        print params, i, threshold, score

        thresholds.append(threshold)
        scores.append(score)
        decisions.append(d)

    if verbose > 0:
        print "[End]", params, np.mean(thresholds), np.mean(scores)

    return (np.mean(scores), np.mean(thresholds), params, thresholds, scores, decisions)
Beispiel #4
0
def draw_cell(cell_info, position):
    rw, rh = calculate_cell_parameters(cell_info)
    rx = position[0] * cell_w + ((cell_w - rw) / 2)
    ry = position[1] * cell_h + ((cell_h - rh) / 2)
    size = rw * rh
    c = int(utils.rescale(size, 0, cell_w * cell_h, 0, num_colors))
    pygame.draw.rect(screen, colors[c], (rx, ry, rw, rh))
Beispiel #5
0
def predict():
    image_data = request.files.get("image")
    image_data_filename = os.path.join(
        app.config["UPLOAD_DIRECTORY"],
        str(token_hex(16)) + os.path.splitext(image_data.filename)[1],
    )
    image_data.save(image_data_filename)

    x = load_img(image_data_filename, target_size=(224, 224))
    x = np.array(img_to_array(x))
    x = rescale(x)

    value = None

    pred = model.predict_proba(x.reshape(1, 224, 224, 3)).flatten()
    print(pred)
    for i in pred:
        if 0 <= i <= 0.2:
            value = "Cat"
        elif 0.99 <= i <= 1:
            value = "Dog"
        else:
            value = "Neither a cat nor a dog"

    return {"message": value}
Beispiel #6
0
def _parallel_eval(Classifier, params, X, y, w, n_repeat=5, verbose=1):
    if verbose > 0:
        print "[Start]", params

    thresholds, scores = [], []

    for i in range(n_repeat):
        if verbose > 0:
            print "Fold", i

        _, X_fold, _, y_fold, _, w_fold = train_test_split(X, y, w, train_size=0.5, random_state=i)
        X_pred = load_predictions("stack/*-fold%d.npy" % i)
        X_fold = np.hstack((X_fold, X_pred))

        X_train, X_valid, y_train, y_valid, w_train, w_valid = train_test_split(X_fold, y_fold, w_fold, train_size=0.33, random_state=i)
        X_train = np.asfortranarray(X_train, dtype=np.float32)

        w_train = rescale(w_train)
        w_train = rebalance(y_train, w_train)

        clf = Classifier(**params)
        try:
            clf = clf.fit(X_train, y_train, sample_weight=w_train)
        except:
            clf = clf.fit(X_train, y_train)

        threshold, score, _ = find_threshold(clf, X_valid, y_valid, w_valid)

        thresholds.append(threshold)
        scores.append(score)

    if verbose > 0:
        print "[End]", params, np.mean(thresholds), np.mean(scores)

    return (np.mean(scores), np.mean(thresholds), params, thresholds, scores)
Beispiel #7
0
def is_sushi(image_file):
    image = rescale(misc.imread(image_file, mode='L'), max_shape=max_shape)
    x_image = image.reshape(1, max_shape[0], max_shape[1], 1)

    logit = sess.run(logits,
                     feed_dict={
                         X: x_image,
                         y: np.array([-1]).reshape(1, 1),
                         keep_prob: 1
                     })[0][0]

    p_sushi = 1 / (1 + np.exp(-logit))

    if p_sushi > 0.8:
        title = "It's sushi! ({0:.0f}% sure)".format(p_sushi * 100)
    elif p_sushi >= 0.5 and p_sushi < 0.8:
        title = "Is it sushi...? ({0:.0f}% sure)".format(p_sushi * 100)
    elif p_sushi < 0.5 and p_sushi > 0.2:
        title = "Is it a sandwich...? ({0:.0f}% sure)".format(
            (1 - p_sushi) * 100)
    else:  #p_sushi < 0.2:
        title = "It's a sandwich! ({0:.0f}% sure)".format((1 - p_sushi) * 100)

    plt.imshow(misc.imread(image_file))
    plt.title(title)
    plt.show()
Beispiel #8
0
def do_ppdrc(fp, filtsize):
    dat = fp.astype('float64')
    dat[np.isnan(dat)] = 0
    dat1 = ppdrc.ppdrc(dat, filtsize)
    dat1 = humutils.rescale(dat1.getdata(), np.min(dat), np.max(dat))
    dat1[np.isnan(fp)] = np.nan
    return dat1
Beispiel #9
0
 def gradX1(self, X1, X2=None):
     # Compute the derivative of the kernel w.r.t to the first argument
     # should return a m * n * d tensor
     X1, X2 = rescale(np.exp(self.log_b_), X1, X2)
     D = diff(X1, X2) # m * n * d array
     K = np.exp(self.log_b_ * 2 - 0.5 * np.sum(np.square(D), axis=-1)) # sum alsong the last axis, which is d
     G = -D * K[:, :, None] / self.log_c_ # G(m, n, d) corresponds to the derivative of of K(m ,n) w.r.t X1(m, d)
     return G
Beispiel #10
0
def msg_received(msg):
    with cmd_mutex:
        scale_func = lambda val: val * 0.5
        if msg.get_type() == LEFT_GO:
            speed = scale_func(msg.get_value())
            controllers['fl'].set_torque_buffered(speed)
            controllers['bl'].set_torque_buffered(speed)

        elif msg.get_type() == RIGHT_GO:
            #Value has to be negated because servos oriented differently
            speed = -scale_func(msg.get_value())
            controllers['fr'].set_torque_buffered(speed)
            controllers['br'].set_torque_buffered(speed)

        elif msg.get_type() == ARM_GO:
            #Value is between 0 and 127. Arm goes from 200-850 (AX12 Units)
            lower = 200
            upper = 850

            result = rescale(msg.get_value(), 0, 127, 0,
                             (upper - lower) / 1023.0 * radians(300))

            controllers['arm_base'].set_position_buffered(result)

        elif msg.get_type() == WRIST_GO:
            #Value is between 0 and 127. Wrist goes from 173 - 820 (AX12 Units)
            lower = 173
            upper = 820

            result = rescale(msg.get_value(), 0, 127, 0,
                             (upper - lower) / 1023.0 * radians(300))

            controllers['arm_wrist'].set_position_buffered(result)

        elif msg.get_type() == HAND_GO:
            #Value is between 0 and 127. Hand goes from 150-870 (AX12 Units)
            lower = 150
            upper = 870

            result = rescale(msg.get_value(), 0, 127, 0,
                             (upper - lower) / 1023.0 * radians(300))

            controllers['arm_hand'].set_position_buffered(result)
        else:
            print 'Unexpected message'
            print str(msg)
Beispiel #11
0
def get_batch(batch_size):
    image_indexes = np.random.randint(0, len(all_images), batch_size)
    images = np.array([
        np.expand_dims(np.array(
            Image.open(os.path.join(DATASET_PATH, all_images[i]))),
                       axis=2) for i in image_indexes
    ])
    return rescale(images.astype(np.float32), -1, 1, data_min=0, data_max=255)
Beispiel #12
0
 def grad(self, X1, X2=None):
     # Compute the derivative w.r.t the kernel hyper parameters
     X1, X2 = rescale(np.exp(self.log_b_), X1, X2)
     D = sqdist(X1, X2)
     K = np.exp(self.log_c_ * 2 - 0.5 * D)
     yield  2 * K # the gradient w.r.t the signal stddev
     if self.iso_:
         yield K * D # the gradient w.r.t the lengscale (square-kernel case)
     else:
         for D in sqdist_foreach(X1, X2):
             yield  K * D # the gradient w.r.t the lengscales (ARD-kernel case)
Beispiel #13
0
def rolling_scores(tested, true_ints=None, show=1000, window=50, rescale=0,
        **kwargs):
    if rescale > 0:
        tested = [(t[0],t[1],ut.rescale(t[2],rescale), t[3]) for t in tested]
    if true_ints:
        tested = cv.tested_from_trues(tested, true_ints)
    padded = list(np.zeros((50,4)))+list(tested)
    rolling = [len([t for t in padded[i:i+window] if t[3]==1])/window for i in range(show)]
    plot(rolling, **kwargs)
    plot([t[2] for t in tested[:show]], **kwargs)
    xlabel('starting index in scored examples')
    ylabel('fraction true in index:index+%s'%window)
    legend(['fraction true','score'])
Beispiel #14
0
def train(Classifier, params, X, y, w, verbose=1):
    if verbose > 0:
        print "[Start]"

    w = rescale(w)
    w = rebalance(y, w)

    clf = Classifier(**params)
    clf.fit(X, y, sample_weight=w)

    if verbose > 0:
        print "[End]"

    return clf
def create_and_store_output(ndim, func, fidelity):
    file_out = Path(
        f'regression_files/output_{ndim}d_{func.name}_{fidelity}.npy')
    if file_out.exists():
        return

    file_in = Path(f'regression_files/input_{ndim}d.npy')
    if not file_in.exists():
        create_and_store_input(ndim)

    x = rescale(np.load(file_in),
                range_in=ValueRange(0, 1),
                range_out=ValueRange(*func.bounds))
    np.save(file_out, func[fidelity](x))
    print(f"output {ndim}d {func.name} created")
Beispiel #16
0
    def save(self, **kwargs):
        """
        image를 썸네일화 하여 저장
        :param kwargs:
        :return:
        """
        image = self.validated_data.pop('image', None)
        if not image:
            return super().save(**kwargs)

        with atomic():
            try:
                super().save(**kwargs)
                resized_image = utils.rescale(data=image.read(), width=200, height=200)
                filename = f"{self.instance.pk}/{image.name}"
                self.instance.image.save(filename, resized_image, save=True)
            except:
                raise ParseError({"error": "이미지 저장에 실패했습니다."})
Beispiel #17
0
def _parallel_eval(Classifier, params, X, y, w, n_repeat=5, verbose=1):
    if verbose > 0:
        print "[Start]", params

    thresholds, scores = [], []

    for i in range(n_repeat):
        if verbose > 0:
            print "Fold", i

        _, X_fold, _, y_fold, _, w_fold = train_test_split(X,
                                                           y,
                                                           w,
                                                           train_size=0.5,
                                                           random_state=i)
        X_pred = load_predictions("stack/*-fold%d.npy" % i)
        X_fold = np.hstack((X_fold, X_pred))

        X_train, X_valid, y_train, y_valid, w_train, w_valid = train_test_split(
            X_fold, y_fold, w_fold, train_size=0.33, random_state=i)
        X_train = np.asfortranarray(X_train, dtype=np.float32)

        w_train = rescale(w_train)
        w_train = rebalance(y_train, w_train)

        clf = Classifier(**params)
        try:
            clf = clf.fit(X_train, y_train, sample_weight=w_train)
        except:
            clf = clf.fit(X_train, y_train)

        threshold, score, _ = find_threshold(clf, X_valid, y_valid, w_valid)

        thresholds.append(threshold)
        scores.append(score)

    if verbose > 0:
        print "[End]", params, np.mean(thresholds), np.mean(scores)

    return (np.mean(scores), np.mean(thresholds), params, thresholds, scores)
Beispiel #18
0
def grad_cam(input_data, model, index, originalImage, activation_function,
             combination, layer_name, n_classes):
    # Define the loss function
    def loss_function(x):
        return categorical_crossentropy(one_hot([index], n_classes), x)

    # Create loss layer
    loss_layer = Lambda(loss_function)(model.output)

    # Build a new model with the loss function.
    model = Model(inputs=model.input, outputs=loss_layer)

    # Extract layer and loss to compute gradient
    conv_layer = model.get_layer(layer_name).output
    loss = sum(model.output)

    # Snagged this code from someplace else
    grads = K.gradients(loss, conv_layer)
    gradient_function = function([model.inputs[0]], [conv_layer, grads])

    # Compute the desired values
    output_values, gradients = gradient_function([input_data])

    output_values = output_values[0]
    gradients = -gradients[0][0]

    # Compute weights according to equation 1
    alphas = np.mean(gradients, axis=(0, 1))

    # Apply combination
    combo = combination(alphas, output_values)

    # Apply activation function
    combo = activation_function(combo)

    # Reshape and rescale feature map
    combo = cv2.resize(combo, (224, 224))
    combo = rescale(combo)

    return combo
    else:
        filename = data_folder + 'control/' + ID
    data, filter = utils.load_edf_file(filename,
                                       channels_to_load,
                                       epoch_duration=5*60)

    if counter == 0:
        n_chans, n_epochs, n_epoch_samples = data["sigbufs"].shape
    output_file_name = output_folder +  ID[:-4] + ".hpf5"
    with h5py.File(output_file_name, "w") as f:
        x = data['sigbufs']
        if simulated_data:
            x = np.random.normal(0, 1, x.shape)
            if group[counter] == 1:
                x = utils.add_known_complex(x, data['fs'])
        x = utils.rescale(x, data['fs'], rescale_mode)
        dset = f.create_dataset("x", data=x, chunks=True)
        f['fs'] = data['fs']
        f["group"] = group[counter]

print("All files processed.")
now = datetime.datetime.now()

with open(output_folder + "conversion.log", "w") as log:
    log.write("Conversion for all files concluded on: " + str(now))
    log.write("\n Utilized channels: " + str(channels_to_load))
    log.write("\n Applied filter: " + filter["type"])
    log.write("\n\t   Low cut: " + str(filter["lowcut"]))
    log.write("\n\t   High cut: " + str(filter["highcut"]))
    log.write("\n Rejection of epochs based on sum of Pxx above 1000.)")
    log.write("\n Signal has been scaled by: " + str(rescale_mode))
Beispiel #20
0
            except:
                print('    Hypnogram error')
                continue

        epoched = np.zeros(
            (int(len(channels_to_load)), int(n_epochs), int(epoch_samples)))
        for i in range(len(channels_to_load)):
            epoched[int(i), :, :] = np.asarray(
                list(zip(*[iter(x[int(i), :])] * int(epoch_samples))))

        if simulated_data:
            x = np.random.normal(0, 1, x.shape)
            if group[counter] == 1:
                x = utils.add_known_complex(x, data['fs'])

        x = utils.rescale(epoched, data['fs'], rescale_mode)

        if cohort == 'SSC':
            stages_to_use = [5, 2]
            stage_names = [
                'wake', 'n1', 'n2', 'n3', 'n4', 'rem', 'unknown', 'artefact'
            ]
            for group, stage in enumerate(stages_to_use):
                output_file_name = output_folder + ID[:-4] + '_' + stage_names[
                    stage] + ".hpf5"
                with h5py.File(output_file_name, "w") as f:
                    dset = f.create_dataset("x",
                                            data=x[:, hypnogram == stage, :],
                                            chunks=True)
                    f['fs'] = data['fs']
                    f["group"] = group
Beispiel #21
0
                                                       w,
                                                       train_size=0.5,
                                                       random_state=i)
    X_pred = load_predictions("stack/*-fold%d.npy" % i)
    X_fold = np.hstack((X_fold, X_pred))

    all_X.append(X_fold)
    all_y.append(y_fold)
    all_w.append(w_fold)

X = np.vstack(all_X)
y = np.concatenate(all_y)
w = np.concatenate(all_w)

clf = Classifier(**params)
w = rescale(w)
w = rebalance(y, w)

try:
    clf.fit(X, y, sample_weight=w)
except:
    clf.fit(X, y)

# And make a submussion
print "Making submission..."
X_test, _, _, _ = load_test()
X_pred = load_predictions("stack/*-test.npy")
X_test = np.hstack((X_test, X_pred))

make_submission(clf, threshold, "output-stacking.csv", X_test=X_test)
Beispiel #22
0
def query_2d(num_id):
    segs = match[num_id]
    project_points = list()
    remainder_points = list()
    remainder_heads = list()
    base_line = segs[0]
    index = df[df['id'] == num_id].index[0]
    df_data = df.loc[index]
    #df_data = df[df['id']==num_id]
    #inv_T = np.zeros((4,4))
    direction = False
    norm_0 = None
    direction_z = None
    ref_z = None
    reflectances_effect = list()
    normals_effect = list()
    depths_effect = list()
    for i, (name, nid) in enumerate(segs):
        print('\rseg: {}/{}'.format(i, len(segs) - 1), end='')
        with open(os.path.join(root, name, "tmp_building.dat"), 'rb') as f:
            seg, bounding = pickle.load(f)
        with open(os.path.join(parse_root, name, "coordinate.dat"), 'rb') as f:
            coor = pickle.load(f)
        with open(os.path.join(parse_root, name, "reflectance.dat"),
                  'rb') as f:
            o_reflectance = pickle.load(f)
        with open(os.path.join(parse_root, name, "normal.dat"), 'rb') as f:
            o_normal = pickle.load(f)
        with open(os.path.join(parse_root, name, "head.dat"), 'rb') as f:
            o_head = pickle.load(f)
        o_depth = generate_depth_image(coor, o_head)

        with open(os.path.join(parse_root, name, "head_info.dat"), 'rb') as f:
            header = pickle.load(f)
        norm, bounding2d, bounding3d, _, _ = bounding[nid]

        bg = np.zeros(coor.shape)
        bg = np.where(coor == bg, 0, 1)
        bg_mask = (bg == 0).all(axis=2)
        bg_mask = np.logical_not(bg_mask)  #背景处为0, 其余为1
        _, bounding2d, _, _, _ = bounding[nid]

        if i == 0:
            base_x, base_y, base_z = header["original_x"], header[
                "original_y"], header["original_z"]
            if bounding3d['x_min'][2] > bounding3d['x_max'][2]:
                direction_z = bounding3d['x_min'] - bounding3d['x_max']
            else:
                direction_z = bounding3d['x_max'] - bounding3d['x_min']
            ref_z = df_data['ref_h'] - header["original_z"]

            mask_seg = np.where(seg == nid, 1, 0).reshape(-1)
            idx_seg = mask_seg.nonzero()[0]
            points_seg = coor.reshape(-1, 3)[idx_seg, :]
            model = ransac.RANSAC()
            m, inlier_mask, _ = model.run(points_seg,
                                          inlier_thres=config.inliers_thres,
                                          max_iterations=config.max_iterations)
            norm_0 = norm
            inliers = points_seg[inlier_mask]

            T0, inv_T0, _b_min, _b_max = calculate_matrix(
                inliers, m, norm_0, ref_z, direction_z)
            facade_line = LineString([(_b_min[0] + base_x, _b_min[1] + base_y),
                                      (_b_max[0] + base_x, _b_max[1] + base_y)
                                      ])
            facade_lines.append(facade_line)
            with open(
                    os.path.join('./', 'tmp_facade_line_{}.txt'.format(MODE)),
                    'a+') as file_handle:  # .txt可以不自己新建,代码会自动新建
                file_handle.write("{}  {}".format(num_id, facade_line))  # 写入
                file_handle.write('\n')

        mask_seg = np.where(seg == nid, 1, 0).reshape(-1)
        idx_seg = mask_seg.nonzero()[0]
        points_seg = coor.reshape(-1, 3)[idx_seg, :]
        points_seg[:, 0] += header["original_x"] - base_x
        points_seg[:, 1] += header["original_y"] - base_y
        points_seg[:, 2] += header["original_z"] - base_z

        home_points_seg = np.ones((points_seg.shape[0], 4), dtype=np.float32)

        home_points_seg[:, 0:3] = points_seg
        proj_points_seg = inv_T0 @ home_points_seg.T
        proj_points_seg = proj_points_seg.T[:, 0:3]

        mask_bb = np.zeros(seg.shape)
        #mask_bb[bounding2d['x_min'].x:bounding2d['x_max'].x,
        #         bounding2d['y_min'].y:bounding2d['y_max'].y] = 1
        adjust = 0
        mask_bb[max(0, bounding2d['x_min'].x -
                    adjust):min(bounding2d['x_max'].x + adjust, seg.shape[0]),
                max(0, bounding2d['y_min'].y -
                    adjust):min(bounding2d['y_max'].y +
                                adjust, seg.shape[1])] = 1

        mask_bb = np.logical_and(mask_bb, bg_mask).reshape(-1)
        idx_bb = mask_bb.nonzero()[0]
        points_bb = coor.reshape(-1, 3)[idx_bb, :]

        reflectance_bb = o_reflectance.reshape(-1, )[idx_bb]
        normal_bb = o_normal.reshape(-1, 3)[idx_bb, :]
        depth_bb = o_depth.reshape(-1, )[idx_bb]

        points_bb[:, 0] += header["original_x"] - base_x
        points_bb[:, 1] += header["original_y"] - base_y
        points_bb[:, 2] += header["original_z"] - base_z

        heads_bb = o_head.reshape(-1, 3)[idx_bb, :]
        heads_bb[:, 0] += header["original_x"] - base_x
        heads_bb[:, 1] += header["original_y"] - base_y
        heads_bb[:, 2] += header["original_z"] - base_z

        home_points_bb = np.ones((points_bb.shape[0], 4), dtype=np.float32)

        home_points_bb[:, 0:3] = points_bb
        proj_points_bb = inv_T0 @ home_points_bb.T
        proj_points_bb = proj_points_bb.T[:, 0:3]

        range_max = np.max(proj_points_seg[:, 0])
        range_min = np.min(proj_points_seg[:, 0])
        mask_effect = np.where(proj_points_bb[:, 0] <= range_max, 1, 0)

        mask_effect = np.where(proj_points_bb[:, 0] >= range_min, mask_effect,
                               0)
        idx_effect = mask_effect.nonzero()[0]
        proj_points = proj_points_bb[idx_effect]
        remainder = points_bb[idx_effect]
        remainder_head = heads_bb[idx_effect]

        reflectances_effect.append(reflectance_bb[idx_effect])
        normals_effect.append(normal_bb[idx_effect])
        depths_effect.append(depth_bb[idx_effect])
        remainder_heads.append(remainder_head)
        remainder_points.append(remainder)
        project_points.append(proj_points)
    print(" ")
    project_points = np.concatenate(project_points, axis=0)
    remainder_points = np.concatenate(remainder_points, axis=0)
    remainder_heads = np.concatenate(remainder_heads, axis=0)

    reflectances_effect = np.concatenate(reflectances_effect, axis=0)
    normals_effect = np.concatenate(normals_effect, axis=0)
    depths_effect = np.concatenate(depths_effect, axis=0)

    path_ply_original = os.path.join(save_root_2d, 'ply_original')
    if not os.path.exists(path_ply_original):
        os.makedirs(path_ply_original)
    #cloud.from_array(remainder_points)
    #pcl.save(cloud, os.path.join(path_ply_original, '{}_original.ply'.format(num_id)), format="ply")

    proj_3d = project_points[:, 0:3]
    average_height = np.median(proj_3d[:, 1])

    index_front_threshold = np.where(proj_3d[:, 1] > average_height - 1.0, 1,
                                     0).nonzero()[0]
    #print("idx: ", index_front_threshold.shape)
    proj_3d = proj_3d[index_front_threshold]
    remainder_points = remainder_points[index_front_threshold]
    remainder_heads = remainder_heads[
        index_front_threshold]  # remove points which behind facade with distance large than 1m.

    reflectances_effect = reflectances_effect[index_front_threshold]
    normals_effect = normals_effect[index_front_threshold]
    depths_effect = depths_effect[index_front_threshold]

    index_under_threshold = np.where(
        remainder_points[:, 2] < np.min(remainder_points[:, 2]) + 2.0, 1,
        0).nonzero()[0]
    index_above_threshold = np.where(
        remainder_points[:, 2] >= np.min(remainder_points[:, 2]) + 2.0, 1,
        0).nonzero()[0]
    under_points = remainder_points[index_under_threshold]
    under_reflectances = reflectances_effect[index_under_threshold]
    under_normals = normals_effect[index_under_threshold]
    under_depths = depths_effect[index_under_threshold]

    under_heads = remainder_heads[index_under_threshold]
    above_heads = remainder_heads[index_above_threshold]

    above_points = remainder_points[index_above_threshold]
    above_reflectances = reflectances_effect[index_above_threshold]
    above_normals = normals_effect[index_above_threshold]
    above_depths = depths_effect[index_above_threshold]

    under_proj_3d = proj_3d[index_under_threshold]
    above_proj_3d = proj_3d[index_above_threshold]

    index_under_behind_threshold = np.where(
        under_proj_3d[:, 1] < average_height + 0.5, 1, 0).nonzero()[0]
    index_under_front_threshold = np.where(
        under_proj_3d[:, 1] >= average_height + 0.5, 1, 0).nonzero()[0]

    under_proj_3d = under_proj_3d[index_under_behind_threshold]
    occlusion_points_1 = under_points[index_under_front_threshold]
    occlusion_heads_1 = under_heads[index_under_front_threshold]

    under_points = under_points[index_under_behind_threshold]
    under_reflectances = under_reflectances[index_under_behind_threshold]
    under_normals = under_normals[index_under_behind_threshold]
    under_depths = under_depths[index_under_behind_threshold]

    index_above_behind_threshold = np.where(
        above_proj_3d[:, 1] < average_height + 2.5, 1, 0).nonzero()[0]
    index_above_front_threshold = np.where(
        above_proj_3d[:, 1] >= average_height + 2.5, 1, 0).nonzero()[0]

    occlusion_points_2 = above_points[index_above_front_threshold]
    occlusion_heads_2 = above_heads[index_above_front_threshold]

    above_proj_3d = above_proj_3d[index_above_behind_threshold]
    above_points = above_points[index_above_behind_threshold]
    above_reflectances = above_reflectances[index_above_behind_threshold]
    above_normals = above_normals[index_above_behind_threshold]
    above_depths = above_depths[index_above_behind_threshold]

    remainder_points = np.concatenate((under_points, above_points), axis=0)
    reflectances = np.concatenate((under_reflectances, above_reflectances),
                                  axis=0)
    normals = np.concatenate((under_normals, above_normals), axis=0)
    depths = np.concatenate((under_depths, above_depths), axis=0)

    m, inlier_mask, flag = model.run(remainder_points,
                                     inlier_thres=config.inliers_thres,
                                     max_iterations=config.max_iterations)
    inliers = remainder_points[inlier_mask]

    T_final, inv_T_final, b_min, b_max = calculate_matrix(
        inliers, m, norm_0, ref_z, direction_z)
    #facade_line = LineString([(b_min[0]+base_x,b_min[1]+base_y), (b_max[0]+base_x,b_max[1]+base_y)])
    #facade_lines.append(facade_line)

    if len(occlusion_points_1) > 0 and len(occlusion_points_2) > 0:
        occlusion_points = np.concatenate(
            [occlusion_points_1, occlusion_points_2])
        occlusion_heads = np.concatenate(
            [occlusion_heads_1, occlusion_heads_2])
    elif len(occlusion_points_1) > 0:
        occlusion_points = occlusion_points_1
        occlusion_heads = occlusion_heads_1
    else:
        occlusion_points = occlusion_points_2
        occlusion_heads = occlusion_heads_2
    '''
    path_ply_occlusion = os.path.join(save_root_2d, 'ply_occlusion')
    if not os.path.exists(path_ply_occlusion):
        os.makedirs(path_ply_occlusion)
    cloud = pcl.PointCloud()
    if len(occlusion_points) > 0:
        cloud.from_array(occlusion_points)
        pcl.save(cloud, os.path.join(path_ply_occlusion, '{}_occlusion.ply'.format(num_id)), format="ply")

    path_ply_o_head = os.path.join(save_root_2d, 'ply_o_head')
    if not os.path.exists(path_ply_o_head):
        os.makedirs(path_ply_o_head)
    cloud = pcl.PointCloud()
    if len(occlusion_heads) > 0:
        cloud.from_array(occlusion_heads)
        pcl.save(cloud, os.path.join(path_ply_o_head, '{}_o_head.ply'.format(num_id)), format="ply")
    
    '''
    intersection_points = intersection_lines_plane(m,
                                                   occlusion_heads,
                                                   occlusion_points,
                                                   p0=inliers[0])
    a, b, c, d = m
    dis_indices = intersection_points[:,
                                      0] * a + intersection_points[:,
                                                                   1] * b + intersection_points[:,
                                                                                                2] * c + d
    dis_inds = np.where(dis_indices < 0.2, 1, 0).nonzero()[0]
    intersection_points = intersection_points[dis_inds]
    '''
    path_ply_intersection = os.path.join(save_root_2d, 'ply_intersection')
    if not os.path.exists(path_ply_intersection):
        os.makedirs(path_ply_intersection)
    cloud = pcl.PointCloud()
    if len(intersection_points) > 0:
        cloud.from_array(intersection_points)
        pcl.save(cloud, os.path.join(path_ply_intersection, '{}_intersection.ply'.format(num_id)), format="ply")
    '''
    #remainder_points = np.concatenate([remainder_points, intersection_points])

    path_ply_in_depth = os.path.join(save_root_2d, 'ply_in_depth')
    if not os.path.exists(path_ply_in_depth):
        os.makedirs(path_ply_in_depth)
    '''
    tmp_remainder_points = remainder_points.copy()
    tmp_remainder_points[:, 0] += base_x
    tmp_remainder_points[:, 1] += base_y
    tmp_remainder_points[:, 2] += base_z
    '''
    #cloud.from_array(remainder_points)
    #pcl.save(cloud, os.path.join(path_ply_in_depth, '{}_in_depth.ply'.format(num_id)), format="ply")

    #m, inlier_mask, flag = model.run(remainder_points, inlier_thres=config.inliers_thres, max_iterations=config.max_iterations)
    #inliers = remainder_points[inlier_mask]
    #if not flag:
    #    T, inv_T = T0, inv_T0
    #else:
    #T_final, inv_T_final, b_min, b_max = calculate_matrix(inliers, m, norm_0, ref_z, direction_z)

    homo_inliers = np.ones((inliers.shape[0], 4), dtype=np.float32)
    homo_inliers[:, 0:3] = inliers
    proj_3d_inliers = inv_T_final @ homo_inliers.T
    proj_3d_inliers = proj_3d_inliers.T[:, 0:3]
    ref_plane = np.mean(proj_3d_inliers[:, 1])
    homo_remainder_points = np.ones((remainder_points.shape[0], 4),
                                    dtype=np.float32)
    homo_remainder_points[:, 0:3] = remainder_points
    proj_3d = inv_T_final @ homo_remainder_points.T
    proj_3d = proj_3d.T[:, 0:3]
    #proj_3d = np.concatenate((under_proj_3d, above_proj_3d), axis=0)

    homo_intersection_points = np.ones((intersection_points.shape[0], 4),
                                       dtype=np.float32)
    homo_intersection_points[:, 0:3] = intersection_points
    proj_intersection_3d = inv_T_final @ homo_intersection_points.T
    proj_intersection_3d = proj_intersection_3d.T[:, 0:3]

    range_x_min = np.min(proj_3d[:, 0])
    range_x_max = np.max(proj_3d[:, 0])
    range_y_min = np.min(proj_3d[:, 2])
    range_y_max = np.max(proj_3d[:, 2])

    #print(range_x_min, range_x_max, range_y_min, range_y_max)
    bg = np.min(proj_3d[:, 1]) - 0.1
    img_2d = np.ones(
        (int(math.ceil(range_y_max - range_y_min) * config.SCALE) + 1,
         int(math.ceil((range_x_max - range_x_min) * config.SCALE)) + 1),
        dtype=np.float32) * bg
    fill_flag = np.zeros(img_2d.shape)
    occlusion_mask = np.zeros(img_2d.shape)
    density_img = np.zeros(img_2d.shape, dtype=np.uint16)
    img_reflectance = np.ones(img_2d.shape) * (np.min(reflectances) - 1)
    img_norm = np.zeros((*img_2d.shape, 3))
    img_original_depth = np.zeros(img_2d.shape)

    density = float(len(proj_3d)) / (img_2d.shape[0] * img_2d.shape[1])

    for i, (x, z, y) in enumerate(proj_3d):

        x = int(round((x - range_x_min) * config.SCALE))
        y = int(round((y - range_y_min) * config.SCALE))

        if fill_flag[img_2d.shape[0]-y-1,x] == 0.5 and \
            abs(img_2d[img_2d.shape[0]-y-1,x]-average_height) < abs(z-average_height):
            pass
        else:
            img_2d[img_2d.shape[0] - y - 1, x] = z
            img_reflectance[img_2d.shape[0] - y - 1, x] = reflectances[i]
            img_norm[img_2d.shape[0] - y - 1, x, :] = normals[i]
            img_original_depth[img_2d.shape[0] - y - 1, x] = depths[i]
        fill_flag[img_2d.shape[0] - y - 1, x] = 0.5
        density_img[img_2d.shape[0] - y - 1, x] += 1

    path_depth_dat = os.path.join(save_root_2d, 'depth_dat')
    if not os.path.exists(path_depth_dat):
        os.makedirs(path_depth_dat)
    with open(os.path.join(path_depth_dat, '{}_depth.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(img_2d.copy(), f)
    path_info = os.path.join(save_root_2d, 'info')
    if not os.path.exists(path_info):
        os.makedirs(path_info)
    with open(os.path.join(path_info, '{}_info.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(
            {
                'size': img_2d.shape,
                'density': density,  # density of point cloud
                'resolution': 1.0 / config.SCALE,
                'left_edge': range_x_min,
                'buttom_edge': range_y_min,
                'trans_i2o': T_final,
                'trans_o2i': inv_T_final,
                'original_x': base_x,
                'original_y': base_y,
                'original_z': base_z,
                'ref_road': ref_z,
                'ref_plane': ref_plane
            },
            f)

    #cloud = pcl.PointCloud()
    #cloud.from_array(remainder_points)
    #pcl.save(cloud, os.path.join(save_root_2d, 'ply', '{}.ply'.format(num_id)), format="ply")

    img_2d_save1 = utils.rescale(img_2d)
    img_2d_save1 = np.round(img_2d_save1 * 255).astype(np.uint8)

    density_img_save1 = utils.rescale(density_img)
    density_img_save1 = np.round(density_img_save1 * 255).astype(np.uint8)

    path_camera_depth = os.path.join(save_root_2d, 'camera_depth')
    if not os.path.exists(path_camera_depth):
        os.makedirs(path_camera_depth)
    with open(os.path.join(path_camera_depth, '{}.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(img_original_depth, f)

    path_normal = os.path.join(save_root_2d, 'normal')
    if not os.path.exists(path_normal):
        os.makedirs(path_normal)
    io.imsave(os.path.join(path_normal, '{}.png'.format(num_id)), img_norm)

    path_reflectance = os.path.join(save_root_2d, 'reflectance')
    if not os.path.exists(path_reflectance):
        os.makedirs(path_reflectance)
    with open(os.path.join(path_reflectance, '{}.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(img_reflectance, f)

    path_depth_img = os.path.join(save_root_2d, 'depth_image')
    if not os.path.exists(path_depth_img):
        os.makedirs(path_depth_img)
    path_density_img = os.path.join(save_root_2d, 'density_image')
    if not os.path.exists(path_density_img):
        os.makedirs(path_density_img)
    path_geometry_image = os.path.join(save_root_2d, 'geometry_image')
    if not os.path.exists(path_geometry_image):
        os.makedirs(path_geometry_image)
    io.imsave(os.path.join(path_depth_img, '{}.png'.format(num_id)),
              img_2d_save1)
    io.imsave(os.path.join(path_density_img, '{}.png'.format(num_id)),
              density_img_save1)
    io.imsave(os.path.join(path_geometry_image, '{}.png'.format(num_id)),
              fill_flag)

    path_occl_mask = os.path.join(save_root_2d, 'occlusion_mask')
    if not os.path.exists(path_occl_mask):
        os.makedirs(path_occl_mask)
    ##### for occlusion compensation

    avg_reflectance = reflectances.mean()
    avg_depth = depths.mean()
    avg_norm = normals.mean(axis=0)
    for i, (x, z, y) in enumerate(proj_intersection_3d):
        if z < -1.0:
            continue
        if x < range_x_min or x > range_x_max:
            continue
        if y < range_y_min or y > range_y_max:
            continue

        x = int(round((x - range_x_min) * config.SCALE))
        y = int(round((y - range_y_min) * config.SCALE))
        occlusion_mask[img_2d.shape[0] - y - 1, x] = 0.5
        '''
            #if (fill_flag[img_2d.shape[0]-y-1,x] != 0 or fill_flag[img_2d.shape[0]-y-1,max(0, x-1)] != 0 or 
            #    fill_flag[img_2d.shape[0]-y-1,min(x-1, img_2d.shape[1]-1)]) != 0:
            if fill_flag[img_2d.shape[0]-y-1,x] != 0:
                continue
            
            if fill_flag[img_2d.shape[0]-y-1,x] == 0.5 and \
                abs(img_2d[img_2d.shape[0]-y-1,x]-average_height) < abs(z-average_height):
                    pass
            else:
                img_2d[img_2d.shape[0]-y-1,x] = z 
                img_reflectance[img_2d.shape[0]-y-1,x] = avg_reflectance
                img_norm[img_2d.shape[0]-y-1,x, :] = avg_norm
                img_original_depth[img_2d.shape[0]-y-1,x] = avg_depth
            fill_flag[img_2d.shape[0]-y-1,x] = 0.5
            density_img[img_2d.shape[0]-y-1,x] += 1
            '''
    io.imsave(os.path.join(path_occl_mask, '{}.png'.format(num_id)),
              occlusion_mask)
    '''    
    path_depth_dat = os.path.join(save_root_2d, 'occl_comp', 'depth_dat')
    if not os.path.exists(path_depth_dat):
        os.makedirs(path_depth_dat)    
    with open(os.path.join(path_depth_dat, '{}_depth.dat'.format(num_id)), 'wb') as f:
        pickle.dump(img_2d.copy(), f)      
        
    img_2d_save2 = utils.rescale(img_2d)
    img_2d_save2 = np.round(img_2d_save2*255).astype(np.uint8)
    
    density_img_save2 = utils.rescale(density_img)
    density_img_save2 = np.round(density_img_save2*255).astype(np.uint8)
    
    
    path_camera_depth = os.path.join(save_root_2d, 'occl_comp', 'camera_depth')
    if not os.path.exists(path_camera_depth):
        os.makedirs(path_camera_depth) 
    with open(os.path.join(path_camera_depth, '{}.dat'.format(num_id)), 'wb') as f:
        pickle.dump(img_original_depth, f)    
        
    path_normal = os.path.join(save_root_2d, 'occl_comp', 'normal')
    if not os.path.exists(path_normal):
        os.makedirs(path_normal) 
    io.imsave(os.path.join(path_normal, '{}.png'.format(num_id)), img_norm)
    
    path_reflectance = os.path.join(save_root_2d, 'occl_comp', 'reflectance')
    if not os.path.exists(path_reflectance):
        os.makedirs(path_reflectance) 
    with open(os.path.join(path_reflectance, '{}.dat'.format(num_id)), 'wb') as f:
        pickle.dump(img_reflectance, f)    
        
    path_depth_img = os.path.join(save_root_2d, 'occl_comp', 'depth_image')
    if not os.path.exists(path_depth_img):
        os.makedirs(path_depth_img)    
    path_density_img = os.path.join(save_root_2d, 'occl_comp', 'density_image')
    if not os.path.exists(path_density_img):
        os.makedirs(path_density_img) 
    path_geometry_image = os.path.join(save_root_2d, 'occl_comp', 'geometry_image')
    if not os.path.exists(path_geometry_image):
        os.makedirs(path_geometry_image)    
    io.imsave(os.path.join(path_depth_img, '{}.png'.format(num_id)), img_2d_save2)
    io.imsave(os.path.join(path_density_img, '{}.png'.format(num_id)), density_img_save2)
    io.imsave(os.path.join(path_geometry_image, '{}.png'.format(num_id)), fill_flag)
    '''
    print("done...{}".format(num_id))
Beispiel #23
0
all_X, all_y, all_w = [], [], []
for i in range(5):
    _, X_fold, _, y_fold, _, w_fold = train_test_split(X, y, w, train_size=0.5, random_state=i)
    X_pred = load_predictions("stack/*-fold%d.npy" % i)
    X_fold = np.hstack((X_fold, X_pred))

    all_X.append(X_fold)
    all_y.append(y_fold)
    all_w.append(w_fold)

X = np.vstack(all_X)
y = np.concatenate(all_y)
w = np.concatenate(all_w)

clf = Classifier(**params)
w = rescale(w)
w = rebalance(y, w)

try:
    clf.fit(X, y, sample_weight=w)
except:
    clf.fit(X, y)


# And make a submussion
print "Making submission..."
X_test, _, _, _ = load_test()
X_pred = load_predictions("stack/*-test.npy")
X_test = np.hstack((X_test, X_pred))

make_submission(clf, threshold, "output-stacking.csv", X_test=X_test)
Beispiel #24
0
 def get(self, X1, X2=None):
     # Compute the kernel between two matrix
     X1, X2 = rescale(np.exp(self.log_b_), X1, X2)
     return np.exp(self.log_b_ * 2 - 0.5 * sqdist(X1, X2))
# Load data
print '\nLoading data...\n'
data = wave.read(song_file)
sample_rate = data[0]
data = numpy.asarray(data[1])
num_samples = len(data)
example_length = frame_length*seq_length

# Normalize?
if normalize == True:
    print '\nNormalizing...\n'
    data_mean = numpy.mean(data)
    data_min = numpy.min(data)
    data_max = numpy.max(data)
    data = rescale(data-data_mean, -1, 1)
    print "subtracted training mean and normalized data to [-1,1]"
else:
    print "using unnormalized data"

# Make sure data will be the right length for reshaping
num_examples = len(data) // example_length
leftover = len(data) % example_length
if leftover < frame_length:
    data_to_use = data[:((num_examples-1)*example_length)+frame_length]
else:
    data_to_use = data[:(num_examples*example_length)+frame_length]

# Reshape
#chars = data.reshape(len(data)/frame_length, frame_length) #list(set(data))
#vocab_size = len(chars)
Beispiel #26
0
G.load_state_dict(
    torch.load("Checkpoints/G_22.pt", map_location=torch.device('cpu')))
GR.load_state_dict(
    torch.load("Checkpoints/GR_7.pt", map_location=torch.device('cpu')))

img = plt.imread(path_i)
faces = detect_face(img)

if len(faces) == 0:
    print("No faces detected")
    exit()

resz = cv2.resize(faces[0], (100, 100))
plt.imsave("out_ld.png", resz)

resz = resz.reshape(1, 100, 100, 3)
resz = np.transpose(resz, (0, 3, 1, 2))
resz = torch.from_numpy(resz)
resz = resz.float()
inp = scale(resz)
out1 = infer(G, inp)
out2 = infer(GR, inp)

inp = rescale(inp)
out1 = rescale(out1)
out2 = rescale(out2)

w = 0.5
out = out1 * w + out2 * (1 - w)
imsave(out[0], "out_hd.png")
Beispiel #27
0
def export_cxs(tested, fname, negmult):
    ut.write_tab_file([(t[0], t[1], ut.rescale(float(t[2]),negmult)) for t in
        tested], fname)
Beispiel #28
0
frm_list = frm_list[0:n_frame]
i = -1
for frm in frm_list:
    i += 1
    found_true = true_detection_list[i]
    found_filtered = []
    found, w = hog_obj.detectMultiScale(frm, **hog_par)
    for r in found:
        inside = False
        for q in found:
            if utils.is_inside(r, q):
                inside = True
                break
        if not inside:
            found_filtered.append(r)
    found_hog = utils.rescale(found_filtered)
    hog_detection_list.append(found_hog)
    if do_draw:
        frm = cv2.cvtColor(frm, cv2.COLOR_GRAY2BGR)
        # utils.draw_detections(frm, found, (0, 0, 255), 1)
        # utils.draw_detections(frm, found_filtered, (0, 0, 255), 3)
        utils.draw_detections(frm, found_hog, (0, 0, 255), 3)
        utils.draw_detections(frm, found_true, (0, 255, 0), 1)
        cv2.imshow('People Detector', frm)
        cv2.waitKey()

"""Performances indices"""
if do_perf:
    precision, recall, f_score = utils.get_performances(hog_detection_list, true_detection_list, n_frame)
    print precision, recall, f_score
Beispiel #29
0
def merge_one_image(depth, density, reflectance):
    reflectance = utils.rescale(reflectance)
    reflectance *= 255
    reflectance = reflectance.astype(np.uint8)
    img = cv2.merge([depth, density, reflectance]) 
    return img
Beispiel #30
0
    teaser['user']['photos'][0]['url']) for teaser in teasers}

for rec in recs:
    user = rec['user']
    rec_id = user['_id']
    print(f"{Fore.GREEN}Analyzing {user['name']} ({rec_id}) ...")

    for rec_photo in user['photos']:
        print(f"\t{Fore.BLUE}Image {rec_photo['url']}")
        rec_img = get_image(rec_photo['url'])

        for (tea_id, tea_img) in teaser_images.items():
            print(
                f"\t\t{Fore.BLACK}{Style.BRIGHT}Comparing with teaser {tea_id}")
            if (compare(rec_img, tea_img)):
                for i, rec_found_img in enumerate([get_image(pic['url']) for pic in user['photos']]):
                    cv2.imshow(f"Rec {i}", rescale(rec_found_img, 30))
                cv2.imshow("Teaser", rescale(tea_img, 30))
                action = cv2.waitKey(0)
                cv2.destroyAllWindows()
                if (action == KEY_LIKE or action == KEY_LIKE - 32):
                    like(rec_id)
                if (action == KEY_PASS or action == KEY_PASS - 32):
                    dislike(rec_id)
                if (action == KEY_SUPERLIKE or action == KEY_SUPERLIKE - 32):
                    superlike(rec_id)
    print("\n")

# Deinit Colorama
deinit()
# Load data
print '\nLoading data...'
data = wave.read(song_file)
sample_rate = data[0]
data = numpy.asarray(data[1])
num_samples = len(data)
example_length = frame_length*seq_length

# Normalize?
if normalize == True:
    print '\nNormalizing -1 to 1...'
    data_mean = numpy.mean(data)
    data_min = numpy.min(data)
    data_max = numpy.max(data)
    data = rescale(data-data_mean, -1, 1, data_min, data_max)
    print 'subtracted training mean and normalized data to [-1,1]'
elif normalize == 'stdev':
    data_stdev = numpy.std(data)
    data = data/data_stdev
    print 'divided by standard deviation'
else:
    print 'using unnormalized data'

# Make sure data will be the right length for reshaping
print '\nGetting examples...'
num_examples = 0
data_to_use = []
if example_shift == False:
    num_examples = len(data)-frame_length // example_length
    data_to_use = data[:num_examples*example_length + frame_length]
Beispiel #32
0
def construct_UP(inp_df,
                 output_size,
                 n_classes=2,
                 P=20,
                 preprocess='inception',
                 np_save_file=None,
                 save_with_indices=None,
                 return_Data=False,
                 log_save=False,
                 conv_to_uint8=True,
                 min_max_exposure=True):
    batch_input = inp_df.copy()  # copy to prevent modifying original df

    images_list = []
    labels_list = []
    nu_list = []
    single_labels = []
    pd_indices = []

    LL_view_list = []
    no_view_list = []
    derivative_view = []

    for idx, row in batch_input.iterrows():
        _dicom, _label, _xmin, _xmax, class_0_fill = row[[
            'dicom', 'label', 'xmin', 'xmax', 'class_0_fill'
        ]]

        a = pydicom.dcmread(_dicom)

        try:
            if a.DerivationDescription:  #derivate view
                derivative_view.append(_dicom)
                continue

        except AttributeError:
            pass

        try:
            c_view = a.ViewPosition
            if c_view == 'LL':
                LL_view_list.append(_dicom)
                continue

            if c_view not in ['PA', 'AP']:
                no_view_list.append(_dicom)
                continue

        except AttributeError:
            continue

        _img = a.pixel_array
        bs = a.BitsStored

        _img = erase_borders(_img)

        if min_max_exposure:

            _img = rescale(_img, 0, 1, np.min(_img),
                           np.max(_img)).astype(np.float32)

            if a.PhotometricInterpretation == 'MONOCHROME1':
                _img = skimage.util.invert(_img, signed_float=False)

            #All pixels ABOVE 99th percentile set TO 99th percentile (getting around super bright labels)
            _img[_img > np.percentile(_img, 99)] = np.percentile(_img, 99)

            #Rescaling again after pixel cutt-off
            _img = rescale(_img, 0, 1, np.min(_img),
                           np.max(_img)).astype(np.float32)

            if conv_to_uint8:
                _img = skimage.img_as_uint(_img)

        else:
            _img = exposure.rescale_intensity(
                _img,
                in_range=('uint' +
                          str(bs)))  #most, if not all, are of type uint16

            if conv_to_uint8:
                _img = skimage.img_as_uint(_img)

            if a.PhotometricInterpretation == 'MONOCHROME1':
                _img = cv2.bitwise_not(_img)

        _img = transform.resize(_img, (output_size, output_size),
                                mode='reflect',
                                anti_aliasing=True)  #img_as_float
        _img = np.stack([_img, _img, _img], axis=-1)
        images_list.append(_img)

        _label_arr = np.zeros((P, P, n_classes))

        single_labels.append([_label])
        _label_arr[..., 0] = class_0_fill

        # For boxed cases
        _label_arr[:, _xmin:_xmax, _label] = 1
        labels_list.append(_label_arr)

        #TODO: change the nu for PTX (0 or 1)
        #all chest cases have laterality, and negatives cases should have class 0 in all patches
        #nu_list.append(_label) # only positives cases with laterality known have nu of 1
        nu_list.append(1)  # all cases have nu of 1.
        #nu_list.append(np.logical_not(_label)) # NL with nu of 1 and PTX with nu of 0
        # nu_list.append(0) # no cases with localization

        pd_indices.append(idx)

    images_list = np.array(images_list, np.float32)
    pd_indices = np.array(pd_indices, np.int32)

    preprocess_methods = {'resnet', 'inception', 'none'}
    if preprocess not in preprocess_methods:
        raise KeyError('Must select a preprocess method from {}'.format(
            preprocess_methods))
    if preprocess == 'inception':
        images_list = (images_list -
                       0.5) * 2  # get values [-1, 1].  Shift and scale.

    if preprocess == 'resnet':  #substract mean instead for Resnet preprocessing
        # RGB values from tensorflow resnet divded by uint8 pixel range (255)
        images_list[..., 0] -= 123.68 / 255
        images_list[..., 1] -= 116.78 / 255
        images_list[..., 2] -= 103.94 / 255

    labels_list = np.array(labels_list, np.float32)
    nu_list = np.array(nu_list, np.float32)

    # single labels as one-hot
    single_labels = sklearn.preprocessing.MultiLabelBinarizer(
        np.arange(n_classes)).fit_transform(single_labels)

    single_labels = np.array(single_labels, np.float32)
    nu_list = nu_list.astype(np.int64)
    #image list, labels, and single_labels list as float32, and nu as int64

    if np_save_file is not None:
        np.savez(np_save_file,
                 images=images_list,
                 labels=labels_list,
                 nus=nu_list,
                 singles=single_labels)

    if save_with_indices is not None:
        np.savez(save_with_indices,
                 images=images_list,
                 labels=labels_list,
                 nus=nu_list,
                 singles=single_labels,
                 pd_indices=pd_indices)

    if log_save:
        with open('LL_view.txt', 'w') as f:
            for i in LL_view_list:
                f.write(i + '\n')

        with open('no_view.txt', 'w') as f:
            for i in no_view_list:
                f.write(i + '\n')

        with open('derivative_view.txt', 'w') as f:
            for i in derivative_view:
                f.write(i + '\n')

    if return_Data:
        return Data_indexed(images_list, labels_list, nu_list, single_labels,
                            pd_indices)
Beispiel #33
0
        # Get the rectangle with the min area
        rect = cv2.minAreaRect(frame)
        frame = cv2.boxPoints(rect)
        frame = np.int0(frame)

        # Get the 2 main angles of the frame
        angles = ut.get_angles(frame)

        rect2 = (rect[0], rect[1], angles[-1])
        img_croped = ut.crop_min_area_rect(color, rect2)
        cv2.imshow("Cropped", img_croped)
        cv2.waitKey(0)

        # As we scale the image, we need to scale back the contour to the original image
        frame_orig = list(
            map(lambda x: ut.rescale(x, size_ini, size_end), frame))

        # Save data to export
        frames.append([ut.rad2deg(angles[-1]), frame_orig])

    # Save the data in a pickle file
    ut.bbox_to_pkl(frames, fname='frames', folder='pkl')
    """
    ##################################  TASK 3: Filter keypoints  ####################################
    """
    # Check for training database file
    if not os.path.exists(PICKLE_MUSEUM_DATASET):
        logger.info("Creating pickle database for museum dataset...")
        db_museum = ut.create_db(TRAIN_MUSEUM_DIR, FEATURES, candidates)
        ut.save_db(db_museum, PICKLE_MUSEUM_DATASET)
    else:
Beispiel #34
0
def calculate_cell_parameters(cell_info):
    brightness = (int(cell_info[0]) + int(cell_info[1]) +
                  int(cell_info[2])) / 3
    width = utils.rescale(brightness, 0, 255, 0, cell_w)
    height = utils.rescale(brightness, 0, 255, 0, cell_h)
    return width, height
Beispiel #35
0
def export_ints(tested, fname, negmult, header):
    ut.write_tab_file([header] + [[t[0], t[1], ut.rescale(float(t[2]),negmult)]
        + list(t[3:]) for t in tested], fname)
Beispiel #36
0
def query_nid(num_id):
    segs = match[num_id]
    project_points = list()
    remainder_points = list()
    base_line = segs[0]
    index = df[df['id'] == num_id].index[0]
    df_data = df.loc[index]
    #df_data = df[df['id']==num_id]
    #inv_T = np.zeros((4,4))
    direction = False
    norm_0 = None
    direction_z = None
    ref_z = None

    for i, (name, nid) in enumerate(segs):
        print('\rseg: {}/{}'.format(i, len(segs) - 1), end='')
        with open(os.path.join(root, name, "tmp_building.dat"), 'rb') as f:
            seg, bounding = pickle.load(f)
        with open(os.path.join(parse_root, name, "coordinate.dat"), 'rb') as f:
            coor = pickle.load(f)
        with open(os.path.join(parse_root, name, "head_info.dat"), 'rb') as f:
            header = pickle.load(f)
        norm, bounding2d, bounding3d, _, _ = bounding[nid]

        bg = np.zeros(coor.shape)
        bg = np.where(coor == bg, 0, 1)
        bg_mask = (bg == 0).all(axis=2)
        bg_mask = np.logical_not(bg_mask)  #背景处为0, 其余为1
        _, bounding2d, _, _, _ = bounding[nid]

        if i == 0:
            base_x, base_y, base_z = header["original_x"], header[
                "original_y"], header["original_z"]
            if bounding3d['x_min'][2] > bounding3d['x_max'][2]:
                direction_z = bounding3d['x_min'] - bounding3d['x_max']
            else:
                direction_z = bounding3d['x_max'] - bounding3d['x_min']
            ref_z = df_data['ref_h'] - header["original_z"]

            mask_seg = np.where(seg == nid, 1, 0).reshape(-1)
            idx_seg = mask_seg.nonzero()[0]
            points_seg = coor.reshape(-1, 3)[idx_seg, :]
            model = ransac.RANSAC()
            m, inlier_mask, _ = model.run(points_seg,
                                          inlier_thres=config.inliers_thres,
                                          max_iterations=config.max_iterations)
            norm_0 = norm
            inliers = points_seg[inlier_mask]

            T0, inv_T0, _, _ = calculate_matrix(inliers, m, norm_0, ref_z,
                                                direction_z)
            '''
            homo_inliers = np.ones((inliers.shape[0],4), dtype=np.float32)
            homo_inliers[:, 0:3] = inliers
            proj_3d = inv_T0 @ homo_inliers.T 
            proj_3d = proj_3d.T[:, 0:3]
            xyz0 = T0[:3]
            xyz0[:, 0] += xyz0[:, 3]
            xyz0[:, 1] += xyz0[:, 3]
            xyz0[:, 2] += xyz0[:, 3]
            xyz0 = xyz0.T
            xyz0_aug = np.ones((xyz0.shape[0], 4))
            xyz0_aug[:, 0:3] = xyz0
            pro_xyz0 = inv_T0 @ xyz0_aug.T
            pro_xyz0 = pro_xyz0.T[:,:3]
            
            fig = plt.figure('graph')
            
            ax = fig.add_subplot(111, projection='3d') 
            
            ax.set_title(r'3d')
            plt.plot(proj_3d[:,0],  proj_3d[:,1], proj_3d[:,2], '.', color='coral')
            plt.plot([pro_xyz0[0, 0], pro_xyz0[3, 0]], [pro_xyz0[0, 1], pro_xyz0[3, 1]], [pro_xyz0[0, 2], pro_xyz0[3, 2]], '-r', label='x')
            plt.plot([pro_xyz0[1, 0], pro_xyz0[3, 0]], [pro_xyz0[1, 1], pro_xyz0[3, 1]], [pro_xyz0[1, 2], pro_xyz0[3, 2]], '-g', label='y')   
            plt.plot([pro_xyz0[2, 0], pro_xyz0[3, 0]], [pro_xyz0[2, 1], pro_xyz0[3, 1]], [pro_xyz0[2, 2], pro_xyz0[3, 2]], '-b', label='z') 
        
            ax.legend()
            ax.set_xlabel('x')
            ax.set_ylabel('y')
            ax.set_zlabel('z')
            ax.text(0, 0, 0, 'x')  # (0,0,0)
            ax.view_init(60,0)
        
            plt.show()
            raise InterruptedError()
            '''

        mask_seg = np.where(seg == nid, 1, 0).reshape(-1)
        idx_seg = mask_seg.nonzero()[0]
        points_seg = coor.reshape(-1, 3)[idx_seg, :]
        points_seg[:, 0] += header["original_x"] - base_x
        points_seg[:, 1] += header["original_y"] - base_y
        points_seg[:, 2] += header["original_z"] - base_z

        home_points_seg = np.ones((points_seg.shape[0], 4), dtype=np.float32)

        home_points_seg[:, 0:3] = points_seg
        proj_points_seg = inv_T0 @ home_points_seg.T
        proj_points_seg = proj_points_seg.T[:, 0:3]

        mask_bb = np.zeros(seg.shape)

        #mask_bb[bounding2d['x_min'].x:bounding2d['x_max'].x,
        #         bounding2d['y_min'].y:bounding2d['y_max'].y] = 1
        adjust = 100
        mask_bb[max(0, bounding2d['x_min'].x -
                    adjust):min(bounding2d['x_max'].x + adjust, seg.shape[0]),
                max(0, bounding2d['y_min'].y -
                    adjust):min(bounding2d['y_max'].y +
                                adjust, seg.shape[1])] = 1

        mask_bb = np.logical_and(mask_bb, bg_mask).reshape(-1)
        idx_bb = mask_bb.nonzero()[0]
        points_bb = coor.reshape(-1, 3)[idx_bb, :]
        points_bb[:, 0] += header["original_x"] - base_x
        points_bb[:, 1] += header["original_y"] - base_y
        points_bb[:, 2] += header["original_z"] - base_z

        home_points_bb = np.ones((points_bb.shape[0], 4), dtype=np.float32)

        home_points_bb[:, 0:3] = points_bb
        proj_points_bb = inv_T0 @ home_points_bb.T
        proj_points_bb = proj_points_bb.T[:, 0:3]

        range_max = np.max(proj_points_seg[:, 0])
        range_min = np.min(proj_points_seg[:, 0])
        mask_effect = np.where(proj_points_bb[:, 0] <= range_max, 1, 0)

        mask_effect = np.where(proj_points_bb[:, 0] >= range_min, mask_effect,
                               0)
        idx_effect = mask_effect.nonzero()[0]
        proj_points = proj_points_bb[idx_effect]
        remainder = points_bb[idx_effect]

        #cloud = pcl.PointCloud()
        #loud.from_array(remainder)
        #pcl.save(cloud, os.path.join(save_root_2d, 'ply', '{}_sub_{}.ply'.format(num_id, i)), format="ply")

        remainder_points.append(remainder)
        project_points.append(proj_points)
    print(" ")
    project_points = np.concatenate(project_points, axis=0)
    remainder_points = np.concatenate(remainder_points, axis=0)

    path_ply_original = os.path.join(save_root_2d, 'ply_original')
    if not os.path.exists(path_ply_original):
        os.makedirs(path_ply_original)
    cloud = pcl.PointCloud()
    '''
    tmp_remainder_points = remainder_points.copy()
    tmp_remainder_points[:, 0] += base_x
    tmp_remainder_points[:, 1] += base_y
    tmp_remainder_points[:, 2] += base_z
    '''
    cloud.from_array(remainder_points)
    pcl.save(cloud,
             os.path.join(path_ply_original, '{}_original.ply'.format(num_id)),
             format="ply")

    proj_3d = project_points[:, 0:3]
    average_height = np.median(proj_3d[:, 1])

    index_front_threshold = np.where(proj_3d[:, 1] > average_height - 1.0, 1,
                                     0).nonzero()[0]
    #print("idx: ", index_front_threshold.shape)
    proj_3d = proj_3d[index_front_threshold]
    remainder_points = remainder_points[index_front_threshold]

    index_under_threshold = np.where(
        remainder_points[:, 2] < np.min(remainder_points[:, 2]) + 2.0, 1,
        0).nonzero()[0]
    index_above_threshold = np.where(
        remainder_points[:, 2] >= np.min(remainder_points[:, 2]) + 2.0, 1,
        0).nonzero()[0]
    under_points = remainder_points[index_under_threshold]
    above_points = remainder_points[index_above_threshold]
    under_proj_3d = proj_3d[index_under_threshold]
    above_proj_3d = proj_3d[index_above_threshold]

    index_under_behind_threshold = np.where(
        under_proj_3d[:, 1] < average_height + 0.5, 1, 0).nonzero()[0]
    under_proj_3d = under_proj_3d[index_under_behind_threshold]
    under_points = under_points[index_under_behind_threshold]
    index_above_behind_threshold = np.where(
        above_proj_3d[:, 1] < average_height + 2.5, 1, 0).nonzero()[0]
    above_proj_3d = above_proj_3d[index_above_behind_threshold]
    above_points = above_points[index_above_behind_threshold]
    remainder_points = np.concatenate((under_points, above_points), axis=0)

    path_ply_in_depth = os.path.join(save_root_2d, 'ply_in_depth')
    if not os.path.exists(path_ply_in_depth):
        os.makedirs(path_ply_in_depth)
    cloud = pcl.PointCloud()
    '''
    tmp_remainder_points = remainder_points.copy()
    tmp_remainder_points[:, 0] += base_x
    tmp_remainder_points[:, 1] += base_y
    tmp_remainder_points[:, 2] += base_z
    '''
    cloud.from_array(remainder_points)
    pcl.save(cloud,
             os.path.join(path_ply_in_depth, '{}_in_depth.ply'.format(num_id)),
             format="ply")

    m, inlier_mask, flag = model.run(remainder_points,
                                     inlier_thres=config.inliers_thres,
                                     max_iterations=config.max_iterations)
    inliers = remainder_points[inlier_mask]
    #if not flag:
    #    T, inv_T = T0, inv_T0
    #else:
    T_final, inv_T_final, b_min, b_max = calculate_matrix(
        inliers, m, norm_0, ref_z, direction_z)

    path_ply_inliers = os.path.join(save_root_2d, 'ply_inliers')
    if not os.path.exists(path_ply_inliers):
        os.makedirs(path_ply_inliers)
    '''
    tmp_inliers = inliers.copy()
    tmp_inliers[:, 0] += base_x
    tmp_inliers[:, 1] += base_y
    tmp_inliers[:, 2] += base_z
    '''
    cloud.from_array(inliers)
    pcl.save(cloud,
             os.path.join(path_ply_inliers, '{}_inliers.ply'.format(num_id)),
             format="ply")

    homo_inliers = np.ones((inliers.shape[0], 4), dtype=np.float32)
    homo_inliers[:, 0:3] = inliers
    proj_3d_inliers = inv_T_final @ homo_inliers.T
    proj_3d_inliers = proj_3d_inliers.T[:, 0:3]
    ref_plane = np.mean(proj_3d_inliers[:, 1])
    homo_remainder_points = np.ones((remainder_points.shape[0], 4),
                                    dtype=np.float32)
    homo_remainder_points[:, 0:3] = remainder_points
    proj_3d = inv_T_final @ homo_remainder_points.T
    proj_3d = proj_3d.T[:, 0:3]
    #proj_3d = np.concatenate((under_proj_3d, above_proj_3d), axis=0)

    range_x_min = np.min(proj_3d[:, 0])
    range_x_max = np.max(proj_3d[:, 0])
    range_y_min = np.min(proj_3d[:, 2])
    range_y_max = np.max(proj_3d[:, 2])

    #print(range_x_min, range_x_max, range_y_min, range_y_max)
    bg = np.min(proj_3d[:, 1]) - 0.1
    img_2d = np.ones(
        (int(math.ceil(range_y_max - range_y_min) * config.SCALE) + 1,
         int(math.ceil((range_x_max - range_x_min) * config.SCALE)) + 1),
        dtype=np.float32) * bg
    fill_flag = np.zeros(img_2d.shape)
    density_img = np.zeros(img_2d.shape, dtype=np.uint16)

    density = float(len(proj_3d)) / (img_2d.shape[0] * img_2d.shape[1])
    for i, (x, z, y) in enumerate(proj_3d):

        x = int(round((x - range_x_min) * config.SCALE))
        y = int(round((y - range_y_min) * config.SCALE))

        if fill_flag[img_2d.shape[0]-y-1,x] == 0.5 and \
            abs(img_2d[img_2d.shape[0]-y-1,x]-average_height) < abs(z-average_height):
            pass
        else:
            img_2d[img_2d.shape[0] - y - 1, x] = z
        fill_flag[img_2d.shape[0] - y - 1, x] = 0.5
        density_img[img_2d.shape[0] - y - 1, x] += 1

    path_depth_dat = os.path.join(save_root_2d, 'depth_dat')
    if not os.path.exists(path_depth_dat):
        os.makedirs(path_depth_dat)
    with open(os.path.join(path_depth_dat, '{}_depth.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(img_2d.copy(), f)
    path_info = os.path.join(save_root_2d, 'info')
    if not os.path.exists(path_info):
        os.makedirs(path_info)
    with open(os.path.join(path_info, '{}_info.dat'.format(num_id)),
              'wb') as f:
        pickle.dump(
            {
                'size': img_2d.shape,
                'density': density,  # density of point cloud
                'resolution': 1.0 / config.SCALE,
                'left_edge': range_x_min,
                'buttom_edge': range_y_min,
                'trans_i2o': T_final,
                'trans_o2i': inv_T_final,
                'original_x': base_x,
                'original_y': base_y,
                'original_z': base_z,
                'ref_road': ref_z,
                'ref_plane': ref_plane
            },
            f)

    #cloud = pcl.PointCloud()
    #cloud.from_array(remainder_points)
    #pcl.save(cloud, os.path.join(save_root_2d, 'ply', '{}.ply'.format(num_id)), format="ply")

    img_2d = utils.rescale(img_2d)
    img_2d = np.round(img_2d * 255).astype(np.uint8)

    density_img = utils.rescale(density_img)
    density_img = np.round(density_img * 255).astype(np.uint8)

    #img_2d = np.where(img_2d<1.0, img_2d, 0.999)
    #img_2d = np.where(img_2d>0.001, img_2d, 0.001)
    #with open(os.path.join(save_root_2d, '{}_project3d.dat'.format(num_id)), 'wb') as f:
    #    pickle.dump(proj_3d, f)
    path_depth_img = os.path.join(save_root_2d, 'depth_image')
    if not os.path.exists(path_depth_img):
        os.makedirs(path_depth_img)
    path_density_img = os.path.join(save_root_2d, 'density_image')
    if not os.path.exists(path_density_img):
        os.makedirs(path_density_img)
    path_geometry_image = os.path.join(save_root_2d, 'geometry_image')
    if not os.path.exists(path_geometry_image):
        os.makedirs(path_geometry_image)
    io.imsave(os.path.join(path_depth_img, '{}.png'.format(num_id)), img_2d)
    io.imsave(os.path.join(path_density_img, '{}.png'.format(num_id)),
              density_img)
    io.imsave(os.path.join(path_geometry_image, '{}.png'.format(num_id)),
              fill_flag)
    print("done...{}".format(num_id))
Beispiel #37
0
def main():
    # Get arguments
    current_folder = os.path.abspath(
        os.path.normpath(os.path.join(__file__, os.path.pardir)))
    in_folder, out_folder = get_args()
    source = os.path.join(current_folder, in_folder)
    target = os.path.join(current_folder, out_folder)

    # Define the target model
    modelname = "vgg"
    model = VGG16(weights="imagenet")
    preprocessor = preprocess_input
    decoder = decode_predictions
    target_layer = "block5_conv3"
    n_classes = 1000

    # Get all image filenames
    filenames = glob.glob(source)

    # Define processing images folder name
    if not os.path.isdir(target): os.mkdir(target)

    errorlog = []

    print("Analysing all provided images.")

    # Loop over all files
    for filename in tqdm(filenames):
        # Extract filename without extension
        name = os.path.basename(filename).split(".")[0]

        # Load the image
        img = image.load_img(filename, target_size=(224, 224))

        # Preprocess the image
        _, ground_truth = process_image(img, preprocess_input)

        # Apply explanation methods
        try:
            backprop, gradcam, squadcam, data = analysis(
                img, model, preprocessor, decoder, target_layer, n_classes)
        except ValueError:
            errorlog.append(
                "ValueError: Image {} -  Expected image size (224, 224). Got {}"
                .format(filename, img.size))

        # Create folder for output if none exists
        if not os.path.isdir(os.path.join(target, name)):
            os.mkdir(os.path.join(target, name))

        # Save explanations and ground truth
        plt.imsave("{}/{}/ground_truth.jpg".format(target, name),
                   rescale(ground_truth))
        plt.imsave(
            "{}/{}/backprop_{}_{}.jpg".format(target, name, modelname,
                                              data[1]), backprop)
        plt.imsave(
            "{}/{}/gradcam_{}_{}.jpg".format(target, name, modelname, data[1]),
            gradcam)
        plt.imsave(
            "{}/{}/squadcam_{}_{}.jpg".format(target, name, modelname,
                                              data[1]), squadcam)
    if errorlog:
        print("Errors:\n{}".format("\n".join(errorlog)))

    print("Analysis completed")
Beispiel #38
0
#
from sklearn.metrics import log_loss
#
import utils
import models
import params

###############################################################################
if __name__ == '__main__':

    np.random.seed(1017)
    target = 'is_iceberg'

    #Load data
    test, test_bands = utils.read_jason(file='test.json', loc='../input/')
    test_X_dup = utils.rescale(test_bands)
    test_meta = test['inc_angle'].values

    tmp = dt.datetime.now().strftime("%Y-%m-%d-%H-%M")
    file_weights = '../weights/weights_current.hdf5'

    if os.path.isfile(file_weights):

        #define and load model
        nb_filters = params.nb_filters
        nb_dense = params.nb_dense
        weights_file = params.weights_file
        model = models.get_model(img_shape=(75, 75, 2),
                                 f=nb_filters,
                                 h=nb_dense)
        model.load_weights(weights_file)
locals().update(config)

# Set up model and prediction function
x = tensor.tensor3('inputs', dtype='float64')
y = tensor.tensor3('targets', dtype='float64')

model = 'bs'
with open ('gru_best.pkl', 'r') as picklefile:
    model = load(picklefile)
y_hat, cost, cells = nn_fprop(x, y, frame_length, hidden_size, num_layers, model)
predict_fn = theano.function([x], y_hat)

# Generate
print "generating audio..."
seed = get_seed(hdf5_file, [seed_index])
sec = 16000
samples_to_generate = sec*secs_to_generate
num_frames_to_generate = samples_to_generate/frame_length + seq_length #don't include seed
predictions = []
prev_input = seed
for i in range(num_frames_to_generate):
    prediction = predict_fn(prev_input)
    predictions.append(prediction)
    pred_min = numpy.min(predictions)
    pred_max = numpy.max(predictions)
    prev_input = rescale(prediction, pred_max, pred_min) 
actually_generated = numpy.asarray(predictions)[seq_length:,:,:,:] #cut off seed
last_frames = actually_generated[:,:,-1,:]
make_wav(output_filename, actually_generated.flatten())
print str(secs_to_generate)+'seconds of audio generated'
print output_filename
 files = get_files(cat)
 matrices = {}
 for fname in sorted(files, key=lambda x: string2prefix_num(x)):
     ######################################################################
     #generate results for current fname
     word_modacolor = get_modacolor_dict(cat, fname)
     print "word_modacolor:", word_modacolor
     word_modacolor = convert_modacolor_to_naturals(word_modacolor)
     print "word_modacolor:", word_modacolor
     #mtx = [[0]*(max_y-min_y+1)]*(max_x-min_x+1) VERY WRONG!! CHECK WHY
     mtx = [[0 for _ in xrange(max_y-min_y+1)] for _ in xrange(max_x-min_x+1)]
     for key, moda in word_modacolor.iteritems():
         x, y = position2coordinates[key+1]
         mtx[x][y] = moda
     
     mtx = rescale(mtx, 10)
     
     #print matrix(mtx)
     #matrices[string2prefix_num(fname)] = ...
     #scipy.misc.imsave(str(string2prefix_num(fname))+'.jpg', matrix(mtx))
     ax = plt.gca()
     ax.patch.set_alpha(0.0)
     
     xtick_locs = [0, 20, 40, 60, 80]
     xtick_labels = [0, 2, 4, 6, 8]
     plt.yticks(xtick_locs, xtick_labels)
     ytick_locs = [0, 100, 200, 300, 400]
     ytick_labels = [0, 10, 20, 30, 40]
     plt.xticks(ytick_locs, ytick_labels)
     
     img = plt.imshow(matrix(mtx))#, cmap=cm.Greys_r) #Needs to be in row,col order