コード例 #1
0
def predict_video(model_class):
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()

        height, width, _ = frame.shape

        if type(model_class) == BasicModel:
            frame_resized = cv2.resize(frame, (224, 224))
            frame_preprocessed = preprocess_input(frame_resized).reshape(
                1, 224, 224, 3)
        else:
            frame_preprocessed = preprocess_input(frame).reshape(
                1, height, width, 3)
        x_scaled, y_scaled = model_class.predict(frame_preprocessed)[0]

        x = int(x_scaled * width)
        y = int(y_scaled * height)

        cv2.circle(frame, (x, y), 5, (0, 0, 255), -1)

        cv2.imshow('frame', frame)

        k = cv2.waitKey(30) & 0xff
        if k == 27:  # press 'ESC' to quit
            break

    cap.release()
    cv2.destroyAllWindows()
コード例 #2
0
def get_preprocessing_func(model_name):
    if model_name == 'resnet_50_v2':
        from tensorflow.keras.applications.resnet_v2 import preprocess_input
    elif model_name == 'vgg16':
        from tensorflow.keras.applications.vgg16 import preprocess_input

    preprocess_input(tf.zeros([4, 32, 32, 3]))

    return lambda x, y: (preprocess_input(x), y)
コード例 #3
0
ファイル: model_training.py プロジェクト: googleinterns/betel
def define_model() -> Tuple[Model, Model]:
    """Defines the architecture of the model."""
    i = Input([None, None, 3], dtype=tf.uint8)
    x = tf.cast(i, tf.float32)
    x = preprocess_input(x)

    # base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(192, 192, 3))
    # base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(192, 192, 3))
    base_model = ResNet152V2(include_top=False,
                             weights='imagenet',
                             input_shape=(192, 192, 3))
    x = base_model(x)

    x = GlobalAveragePooling2D()(x)
    x = Dense(512, kernel_regularizer='l2')(x)
    x = BatchNormalization()(x)
    x = ReLU()(x)
    # x = LayerNormalization()(x)
    # x = Dense(256, kernel_regularizer='l2')(x)
    # x = BatchNormalization()(x)
    # x = ReLU()(x)
    # x = LayerNormalization()(x)
    x = Dense(64, kernel_regularizer='l2')(x)
    x = BatchNormalization()(x)
    x = ReLU()(x)
    # x = LayerNormalization()(x)
    predictions = Dense(1, activation='sigmoid', kernel_regularizer='l2')(x)

    model = Model(inputs=[i], outputs=predictions)

    return model, base_model
コード例 #4
0
ファイル: task_3_2.py プロジェクト: kinoai/skyhacks2019
def train(images, labels):
    model = prepare_model()

    checkpoint = ModelCheckpoint('models/model_task_3_2.h5', monitor='val_loss', verbose=1, mode='min', save_best_only=True)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2, min_lr=0.001)
    callbacks_list = [checkpoint, reduce_lr]

    images = preprocess_input(images)

    def encode_ordinal(class_val):
        return [
            1 if class_val > 1 else 0,
            1 if class_val > 2 else 0,
            1 if class_val > 3 else 0
        ]
    labels = np.array(list(map(encode_ordinal, labels)))

    # Shuffle
    randomize = np.arange(len(images))
    np.random.shuffle(randomize)
    images = images[randomize]
    labels = labels[randomize]

    model.fit(x=images, y=labels, epochs=15, batch_size=64, validation_split=0.3, shuffle=False, callbacks=callbacks_list)

    # model.save('adiz_trained.h5')

    labels = model.predict(images)

    scores = model.evaluate(images, labels, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]))
コード例 #5
0
def detect_and_predict_mask(frame, faceNet, maskNet):
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(frame, 1.0, (800, 800), (104.0, 177.0, 123.0))

    faceNet.setInput(blob)
    detections = faceNet.forward()
    faces = []
    locs = []
    preds = []

    for i in range(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]

        if confidence > args.confidence:
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            (startX, startY) = (max(0, startX), max(0, startY))
            (endX, endY) = (min(w - 1, endX), min(h - 1, endY))

            face = frame[startY:endY, startX:endX]
            face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            face = cv2.resize(face, (224, 224))
            face = img_to_array(face)
            face = preprocess_input(face)

            faces.append(face)
            locs.append((startX, startY, endX, endY))

    if len(faces) > 0:
        faces = np.array(faces, dtype="float32")
        preds = maskNet.predict(faces, batch_size=32)

    return (locs, preds)
コード例 #6
0
def preprocess(image):
    image = tf.io.decode_png(tf.io.read_file(image), channels=3)
    image = tf.image.resize_with_pad(image=image,
                                     target_height=HEIGHT,
                                     target_width=WIDTH)
    image = resnet_v2.preprocess_input(image)
    return image
コード例 #7
0
def build_model(classes=2):
    inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
    x = preprocess_input(inputs)
    x = ResNet152V2(weights=None, classes=classes)(x)
    model = Model(inputs=inputs, outputs=x)
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
    return model
コード例 #8
0
def generate_integrated_grad(input_image):
    arr = np.array(input_image)
    image = preprocess_input(cv2.resize(arr, (224, 224)))
    race = encode_image(integrated_grad_PIL(image, "race"))
    gender = encode_image(integrated_grad_PIL(image, "gender"))
    age = encode_image(integrated_grad_PIL(image, "age"))
    return race, gender, age
コード例 #9
0
def predict(input_image):
    arr = np.array(input_image)
    # print(preprocess_input(arr), file=sys.stderr)
    image = preprocess_input(cv2.resize(arr,
                                        (224, 224))).reshape(-1, 224, 224, 3)
    # Race
    race_dict = {
        0: 'Black',
        1: 'East Asian',
        2: 'Latino/Hispanic',
        3: 'Indian',
        4: 'Middle Eastern',
        5: 'SE Asian',
        6: 'White'
    }
    race_model = load_model('models/race_v6.hdf5')
    race_pred = race_model.predict(image)
    race = race_dict[np.argmax(race_pred)]

    race_percent = list(map(lambda x: round(x * 100), race_pred[0]))
    race_results = []
    for i in range(len(race_percent)):
        race_results.append({"cat": race_dict[i], "val": race_percent[i]})

    # Gender
    gender_dict = {0: "Female", 1: "Male"}
    gender_model = load_model('models/gender_v1.hdf5')
    gender_pred = gender_model.predict(image)
    gender = gender_dict[np.argmax(gender_pred)]

    gender_percent = list(map(lambda x: round(x * 100), gender_pred[0]))
    gender_results = []
    for i in range(len(gender_percent)):
        gender_results.append({
            "cat": gender_dict[i],
            "val": gender_percent[i]
        })

    # Age
    age_dict = {
        0: "0-2",
        1: "10-19",
        2: "20-29",
        3: "3-9",
        4: "30-39",
        5: "40-49",
        6: "50-59",
        7: "60-69",
        8: "more than 70"
    }
    age_model = load_model('models/age_v1.hdf5')
    age_pred = age_model.predict(image)
    age = age_dict[np.argmax(age_pred)]

    age_percent = list(map(lambda x: round(x * 100), age_pred[0]))
    age_results = []
    for i in range(len(age_percent)):
        age_results.append({"cat": age_dict[i], "val": age_percent[i]})

    return race, gender, age, race_results, gender_results, age_results
コード例 #10
0
ファイル: utils.py プロジェクト: Mikehem/GradCam
def preprocess(im):
    #im = img_to_array(load_img(os.path.join(SAMPLE_DIR,filename),target_size = TARGET_SIZE))
    x = keras_image.img_to_array(im)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    return x
コード例 #11
0
    def get_data_loader(data : tuple, data_subset_mode='train', batch_size=32, num_classes=None, infinite=True, augment=True, seed=2836):

        num_samples = len(data[0])
        x = tf.data.Dataset.from_tensor_slices(data[0])
        labels = tf.data.Dataset.from_tensor_slices(data[1])
        data = tf.data.Dataset.zip((x, labels))

        data = data.cache()
        if data_subset_mode == 'train':
            data = data.shuffle(buffer_size=num_samples)

        # data = data.map(lambda x,y: (tf.image.convert_image_dtype(load_img(x)*255.0,dtype=tf.uint8),y), num_parallel_calls=-1)
        # data = data.map(load_example, num_parallel_calls=AUTOTUNE)
        data = data.map(load_example, num_parallel_calls=AUTOTUNE)


        data = data.map(lambda x,y: (preprocess_input(x), y), num_parallel_calls=AUTOTUNE)

        if infinite:
            data = data.repeat()

        if data_subset_mode == 'train':
            data = data.shuffle(buffer_size=200, seed=seed)
            augmentor = TRAIN_image_augmentor
        elif data_subset_mode == 'val':
            augmentor = VAL_image_augmentor
        elif data_subset_mode == 'test':
            augmentor = TEST_image_augmentor

        if augment:
            data = augmentor.apply_augmentations(data)

        data = data.batch(batch_size, drop_remainder=True)

        return data.prefetch(AUTOTUNE)
コード例 #12
0
    def predict(self):
        # load weights
        resnet_model.load_weights(self.weights)

        # in case of rbg image
        if self.rgb:
            image = plt.imread(self.image)
            x = cv2.resize(image, (224, 224))
        # in case of brg image
        else:
            x = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
        # preprocess image for resnet model
        p_image = preprocess_input(x)
        # add batch
        input_image = p_image[None, ...]
        # predict emotion
        predict = resnet_model.predict_classes(input_image)

        # list of emotion in alphabetical order
        emo = [
            'anger', 'contempt', 'disgust', 'fear', 'happy', 'neutral', 'sad',
            'surprise', 'uncertain'
        ]

        return emo[int(predict)]
コード例 #13
0
def prepare_image(image):
    if image.mode != 'RGB':
        image = image.convert("RGB")
    image = image.resize((224, 224))
    image_tensor = img_to_array(image)
    preprocessed_img = preprocess_input(image_tensor)
    preprocessed_img = np.expand_dims(preprocessed_img, axis=0)
    return preprocessed_img
コード例 #14
0
def simple_decode(img):
    img = tf.image.decode_jpeg(img, channels=3)
    #img = tf.image.convert_image_dtype(img, tf.float32)  # NOTE: Must do this before other operations or can mangle img
    # TODO the above was causing issues? I don't understand anymore...
    # TODO I know it should cause issues with mixed precision but why was it handicaping performance?
    img = tf.image.resize(img, [settings.IMG_WIDTH, settings.IMG_HEIGHT])
    img = preprocess_input(img)  # NOTE: This does A TON for accuracy
    #img = tf.image.convert_image_dtype(img, tf.float32)  #TODO remove this if preproces sis used
    return img
コード例 #15
0
def get_preprocessing_func(model_name):
    if model_name.startswith('resnet'):
        from tensorflow.keras.applications.resnet_v2 import preprocess_input
    elif model_name == 'vgg16':
        from tensorflow.keras.applications.vgg16 import preprocess_input
    elif model_name == 'shallow':
        def preprocess_input(x):
            return ((x/255.0)-0.5)*2.0

    return lambda x, y: (preprocess_input(x),y)
def prep(img):

    if deb: print('check uint8:', img[0][0, :10, :2])
    print(img.shape)
    img = crop_center_square(img)
    print(img.shape)
    img = tf.image.resize(img, (224, 224))  # img[:,:,20:300]
    print(img.shape)
    img = preprocess_input(img)
    if deb: print('after preprocess: ', img[0][0, :10])
    #print('np' ,img.shape)
    return img
コード例 #17
0
 def format_image(self, image):
     """Resize the images to a fixed input size, and
     rescale the input channels to a range of [-1, 1].
     (According to https://www.tensorflow.org/tutorials/images/transfer_learning)
     """
     image = tf.cast(image, tf.float32)
     #       \/ does the same #  image = (image / 127.5) - 1
     image = preprocess_input(
         image
     )  # https://github.com/keras-team/keras-applications/blob/master/keras_applications/imagenet_utils.py#L152
     image = tf.image.resize(image, (self.IMG_SIZE, self.IMG_SIZE))
     return image
コード例 #18
0
 def do(images):
     images = resnet_v2.preprocess_input(np.asarray(images))
     model = self.resnet(include_top=False,
                         weights='imagenet',
                         input_tensor=None,
                         input_shape=images[0].shape,
                         pooling=None,
                         classes=None, # ignored when include_top False
                         classifier_activation=None, # ignored when include_top False
                        )
     feature_maps = model.predict(images)
     tf.keras.backend.clear_session()
     return images
コード例 #19
0
 def map_decode_fn(images):
     images = tf.io.decode_jpeg(images)
     images = tf.image.resize(images,
                              (MLConfig.INPUT_WIDTH, MLConfig.INPUT_HEIGHT),
                              method="nearest")
     images.set_shape((MLConfig.INPUT_WIDTH, MLConfig.INPUT_HEIGHT, 3))
     images = tf.cast(images, tf.float32)
     if MLConfig.BACKBONE == "resnet":
         images = resnet_v2.preprocess_input(images)
     elif MLConfig.BACKBONE == "efficientnet":
         images = efficientnet.preprocess_input(images)
     else:
         raise ValueError()
     return tf.cast(images, tf.float32)
コード例 #20
0
def decode_img(img):
    # convert the compressed string to a 3D uint8 tensor

    # img = tf.image.decode_jpeg(img)
    img = tf.image.decode_jpeg(img, channels=3)
    #img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [settings.IMG_WIDTH, settings.IMG_HEIGHT])
    # img = tf.image.decode_image(img)
    # img = tf.image.decode_jpeg(img)
    # img = tf.image.decode_image(img, channels=0)
    # img = tf.image.decode_jpeg(img, channels=0)
    # img = tf.image.resize(img, [IMG_WIDTH*2, IMG_HEIGHT*2])
    # img = zoom(img)

    # st.write(img)

    # img = tf.image.convert_image_dtype(img, tf.float32)

    #NUM_BOXES = 3
    #boxes = tf.random.uniform(shape=(NUM_BOXES, 3))
    #box_indices = tf.random.uniform(shape=(NUM_BOXES,), minval=0, maxval=settings.BATCH_SIZE, dtype=tf.int32)
    #img = tf.image.crop_and_resize(img, boxes, box_indices, (settings.IMG_HEIGHT, settings.IMG_WIDTH))
    # st.write(img)
    # Use `convert_image_dtype` to convert to floats in the [0,1] range.
    # resize the image to the desired size.
    # img = tf.image.random_crop(img, [IMG_WIDTH, IMG_HEIGHT, 3])
    img = tf.image.random_flip_left_right(img)
    img = tf.image.random_flip_up_down(img)
    img = tf.image.rot90(
        img, tf.random.uniform(shape=[], minval=0, maxval=4, dtype=tf.int32))
    # TODO test if this is corrupting jpg
    img = tf.image.random_hue(img, 0.02)
    #img = tf.image.random_saturation(img, 0.6, 1.6)
    img = tf.image.random_brightness(img, 0.02)
    img = tf.image.random_contrast(img, 0.02, 0.05)

    # img = tf.image.convert_image_dtype(img, tf.float16)

    # img = tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])
    img = preprocess_input(img)  # This handles float conversion
    # img = tf.image.resize(img, [settings.IMG_WIDTH, settings.IMG_HEIGHT])
    #img = tf.image.convert_image_dtype(img, tf.float32)  #TODO remove this if preproces sis used

    # st.write(img)
    return img
コード例 #21
0
    def __getitem__(self, index):
        'Generate one batch of data'
        # selects indices of data for next batch
        indexes = self.indexes[index * self.batch_size:(index + 1) *
                               self.batch_size]

        # select data and load images
        y = np.array([self.y[k] for k in indexes])
        X = [
            np.array(image.load_img(self.X[k], target_size=self.dim))
            for k in indexes
        ]

        if self.augmentor != None:
            X = self.augmentor(X)

        X = np.array([preprocess_input(x) for x in X])
        return X, y
コード例 #22
0
def get_image(filename: str, size: tuple = (212, 320)):
    """
    Function to load image as tensor and resize it to specific size

    Args:
        filename: file name (path)
        size: tuple (height, width) with size of image after resizing

    Returns:
        Image as tf.Tensor
    """
    image = tf.io.read_file(filename)
    image = tf.image.decode_jpeg(image)
    # Use cast instead of convert_image_dtype to avoid rescaling
    image = tf.cast(image, tf.float32)
    image = tf.image.resize_with_crop_or_pad(image,
                                             target_height=size[0],
                                             target_width=size[1])
    image = preprocess_input(image)
    return image
コード例 #23
0
ファイル: nets.py プロジェクト: dolokov/multitracker
def EncoderPretrained(config, inputs):
    if not 'kp_backbone' in config:
        config['kp_backbone'] = 'resnet'

    if config['kp_backbone'] == "resnet":
        from tensorflow.keras.applications.resnet_v2 import preprocess_input
    elif "efficientnet" in config['kp_backbone']:
        from tensorflow.keras.applications.efficientnet import preprocess_input

    if config['kp_backbone'] == "resnet":
        net = tf.keras.applications.ResNet152V2
    elif config['kp_backbone'] == 'efficientnet':
        net = tf.keras.applications.EfficientNetB7

    inputss = preprocess_input(inputs)
    net = net(input_tensor=inputss,
              include_top=False,
              weights='imagenet',
              pooling='avg')

    for i, layer in enumerate(net.layers):
        #print('layer',layer.name,i,layer.output.shape)
        layer.trainable = False

    if config['kp_backbone'] == "resnet":
        layer_name = [
            'conv3_block8_1_relu', "conv3_block8_preact_relu",
            "max_pooling2d_1"
        ][1]  # 32x32
        layer_name = 'conv2_block3_1_relu'  # 64x64x64
    elif config['kp_backbone'] == "efficientnet":
        layer_name = ['conv3_block8_1_relu'][0]

    feature_activation = net.get_layer(layer_name)
    model = tf.keras.models.Model(name="ImageNet Encoder",
                                  inputs=net.input,
                                  outputs=[feature_activation.output])
    return model
コード例 #24
0
    def tag_file(file_name):
        """
        This method classifies one given image.
        :param file_name: Path to the file, that should be classified.
        :return: Tags as list of strings
        """

        f = open('labels.txt', 'w+')

        res = []
        if file_name.endswith('.jpeg') or file_name.endswith('.jpg'):

            # preprocess an image
            img = tf_image.load_img(file_name,
                                    target_size=Tagger.input_image_size[:2])
            img = tf_image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = preprocess_input(img)
            # apply NN and make a prediction
            predictions = Tagger.model.predict(img)
            # decode the results into a list of tuples (class, description, probability)
            # (one such list for each sample in the batch)

            translated_predictions = decode_predictions(predictions, top=2)[0]

            if float(translated_predictions[0][2]) - float(
                    translated_predictions[1][2]) <= 0.08:
                res = [
                    translated_predictions[0][1], translated_predictions[1][1]
                ]
                tf.keras.backend.print_tensor(Tagger.model.layers[-1].output)
            else:
                res = [translated_predictions[0][1]]

        f.close()
        return res
コード例 #25
0
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.applications import InceptionV3
import time

# Import train data
train_imagePaths = glob.glob('data/data/train/*/*')
train_labels = []
train_images = []
print("=> Loading Training images")
for i,imagePath in enumerate(train_imagePaths):
    train_labels.append(imagePath.split('/')[-2])
    image = cv2.imread(imagePath)
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (224, 224))
    image = preprocess_input(image)
    # image = np.repeat(image[..., np.newaxis], 3, -1)
    train_images.append(image)
    
train_images = np.array(train_images)
train_labels = np.array(train_labels)
print("=> Loaded {} train images".format(train_images.shape[0]))
lb = LabelBinarizer()
train_labels = lb.fit_transform(train_labels)

# Import validation data
val_imagePaths = glob.glob('data/data/val/*/*')
val_labels = []
val_images = []
print("=> Loading Validation images")
for i,imagePath in enumerate(val_imagePaths):
コード例 #26
0
def augment_dataset(project_id):
    config = model.get_config(project_id)
    config['input_image_shape'] = cv.imread(glob(os.path.join(config['data_dir'],'train/*.png'))[0]).shape[:2]
    h = config['input_image_shape'][0]
    w = config['input_image_shape'][1] // (2+ len(config['keypoint_names'])//3)
    print(config)
    print(h,w, 4 * w)
    
    file_list, dataset_train = model.load_raw_dataset(config,'train')
    inputs = tf.keras.layers.Input(shape=[config['img_height'], config['img_width'], 3])
    inputss = preprocess_input(inputs)
    net = tf.keras.applications.ResNet152V2(input_tensor=inputss,
            include_top=False,
            weights='imagenet',
            pooling='avg')
    net.summary()
    if 0:
        for layer in net.layers:
            try:
                print(layer.name,layer.outputs[0].shape)
            except:
                pass 

    layer_name = 'conv1_conv'
    feature_activation = net.get_layer(layer_name)
    feature_extractor = tf.keras.models.Model(name="ImageNet Encoder",inputs=net.input,outputs=[feature_activation.output])
    
    output_dir = '/tmp/feature_augment'
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    files = [f for f in sorted(glob(os.path.join(config['data_dir'],'train/*.png'))) if not 'augment-' in f]
    for i,f in enumerate(files):
        im = cv.imread(f)
        rgb = im[:,:w,:]
        rgb = rgb.reshape((1,) + rgb.shape)
        rgb = preprocess_input(rgb)
        features = feature_extractor(rgb,training=False).numpy()[0,:,:,:]
        features = cv.resize(features,None,None,fx=2.,fy=2.)

        print('[*] wrote %i/%i:'%(i,len(files)),im.shape,features.shape,features.min(),features.max())

        for j in range(2):
            fo = os.path.join(config['data_dir'],'train/augment-%i-%i.png' % (i,j) )
            #print(fo)
            a = np.zeros_like(features[:,:,0])
            b = np.zeros_like(features[:,:,0])
            c = np.zeros_like(features[:,:,0])
            for k in range(features.shape[-1] // 3):
                a += features[:,:,int(np.random.uniform(features.shape[-1]))]
                b += features[:,:,int(np.random.uniform(features.shape[-1]))]
                c += features[:,:,int(np.random.uniform(features.shape[-1]))]
            abc = cv.merge((a,b,c))
            thresh = 1. 
            abc[thresh > np.abs(abc)] = thresh 
            abc8 = 255. * (abc-abc.min())/(1e-5+abc.max()-abc.min())
            abc8 = np.uint8(abc8)
            abc8 = np.hstack((abc8,im[:,w:,:]))
            cv.imwrite(fo,abc8)

    '''for x,y in dataset_train:
コード例 #27
0
def grad_cam():

    # Read input
    req_data = request.get_json()

    # Label contains filename (not full path)
    label = req_data['label']
    # img (preprocessed?)
    img = req_data['image']

    # Get label index
    label = label.split('_')[0] + '_' + label.split('_')[1]
    label_idx = np.where(np.array(model.classes)[:, 0] == label)[0][0]

    # # Load and prepare (normalize) image
    img = image.img_to_array(img)
    img = tf.image.resize_with_crop_or_pad(img, 224, 224)
    img_to_plot = img / 255.0
    img = preprocess_input(img)
    img = img.numpy()

    # Reshape image to (batch_size, heigh, width, channel)
    img = img.reshape(-1, *img.shape)

    # Gradient model, outputs tuple with:
    # - output of conv layer
    # - output of head layer
    grad_model = tf.keras.models.Model(
        [model.model.inputs],
        [model.model.get_layer('conv5_block3_3_conv').output, model.model.output])

    # Run model and record outputs, loss, and gradients
    with tf.GradientTape() as tape:
        conv_outputs, predictions = grad_model(img)
        loss = predictions[:, label_idx]

    # Output of conv layer
    output = conv_outputs[0]

    # Gradients of loss wrt. conv layer
    grads = tape.gradient(loss, conv_outputs)[0]

    # Guided Backprop (elimination of negative values)
    gate_f = tf.cast(output > 0, 'float32')
    gate_r = tf.cast(grads > 0, 'float32')
    guided_grads = gate_f * gate_r * grads

    # Average weight of filters
    weights = tf.reduce_mean(guided_grads, axis=(0, 1))

    # Class activation map (cam)
    # Multiply values of conv filters with gradient weights
    cam = tf.reduce_sum(output * weights, axis=2)

    # Rescale to org image size and min-max scale
    cam = cv2.resize(cam.numpy(), (224, 224))
    cam = np.maximum(cam, 0)
    heatmap = (cam - cam.min()) / (cam.max() - cam.min())

    # Original image, reshape back, denormalize and adjust type to heatmap
    src_1 = img_to_plot
    src_1 = np.uint8(255 * src_1)
    src_1 = cv2.cvtColor(src_1, cv2.COLOR_RGB2BGR)

    # Define color map based on heatmap
    src_2 = np.uint8(255 * heatmap)
    src_2 = cv2.applyColorMap(src_2, cv2.COLORMAP_RAINBOW)

    # Calculates the weighted sum of two arrays:
    # dst = src1*alpha + src2*beta + gamma
    output_image = cv2.addWeighted(src1=src_1, alpha=0.5, src2=src_2, beta=0.8, gamma=0)

    output_dict = {"heatmap": output_image.tolist()}
    return jsonify(output_dict)
コード例 #28
0
    datasets = {
                'PNAS': pnas_dataset.PNASDataset(src_db=src_db),
                'Leaves': leaves_dataset.LeavesDataset(src_db=src_db),
                'Fossil': fossil_dataset.FossilDataset(src_db=src_db)
                }
    data = datasets[PARAMS['dataset_name']]
    data_config = stuf(threshold=0,
                       data_splits_meta={
                                         'train':PARAMS['train_size'],
                                         'val':PARAMS['val_size'],
                                         'test':PARAMS['test_size']
                                        }
                       )

    preprocess_input = get_preprocessing_func(PARAMS['model_name'])
    preprocess_input(tf.zeros([4, 32, 32, 3]), tf.zeros([4, 32]))
    load_example = partial(_load_example, img_size=PARAMS['image_size'], num_classes=data.num_classes)



    # class ConfusionMatrixCallback(Callback):
    #
    #     def __init__(self, log_dir, val_imgs, val_labels, classes, freq=1, seed=None):
    #         self.file_writer = tf.contrib.summary.create_file_writer(log_dir)
    #         self.log_dir = log_dir
    #         self.seed = seed
    #         self._counter = 0
    #         self.val_imgs = val_imgs
    #
    #         if val_labels.ndim==2:
    #             val_labels = tf.argmax(val_labels,axis=1)
コード例 #29
0
    all_images = pickle.load(f)

with open(path.join(DATA_PATH, 'labels'), 'rb') as f:
    all_targets = pickle.load(f)

with open(path.join(DATA_PATH, 'validation'), 'rb') as f:
    validation_images = pickle.load(f)

with open(path.join(DATA_PATH, 'validation_labels'), 'rb') as f:
    validation_targets = pickle.load(f)

all_images = np.concatenate([all_images, validation_images], axis=0)
all_targets = np.concatenate([all_targets, validation_targets], axis=0)

all_images = np.array(all_images)
all_images = preprocess_input(all_images)

all_targets = all_targets[:, 4:]
all_targets = all_targets.astype(int)

labels_task_1 = [
    'Bathroom', 'Bathroom cabinet', 'Bathroom sink', 'Bathtub', 'Bed',
    'Bed frame', 'Bed sheet', 'Bedroom', 'Cabinetry', 'Ceiling', 'Chair',
    'Chandelier', 'Chest of drawers', 'Coffee table', 'Couch', 'Countertop',
    'Cupboard', 'Curtain', 'Dining room', 'Door', 'Drawer', 'Facade',
    'Fireplace', 'Floor', 'Furniture', 'Grass', 'Hardwood', 'House', 'Kitchen',
    'Kitchen & dining room table', 'Kitchen stove', 'Living room', 'Mattress',
    'Nightstand', 'Plumbing fixture', 'Property', 'Real estate',
    'Refrigerator', 'Roof', 'Room', 'Rural area', 'Shower', 'Sink', 'Sky',
    'Table', 'Tablecloth', 'Tap', 'Tile', 'Toilet', 'Tree', 'Urban area',
    'Wall', 'Window'
コード例 #30
0
ファイル: resnet.py プロジェクト: lineality/data-science
def preprocess(img):
    arr = image.img_to_array(img)
    expanded = numpy.expand_dims(arr, axis=0)
    preprocessed = preprocess_input(expanded)
    return (preprocessed)