def preprocess_data(self):
        self.x_train = utils.preprocess_input(self.x_train)
        self.x_test = utils.preprocess_input(self.x_test)

        # Preprocess labels (y) data
        self.y_train = keras.utils.to_categorical(self.y_train,
                                                  constants.NUM_CLASSES)
        self.y_test = keras.utils.to_categorical(self.y_test,
                                                 constants.NUM_CLASSES)
Пример #2
0
    def process_image(self,image_path,waitTime=0):

        emotion_labels = get_labels('fer2013')
        gender_labels = get_labels('imdb')
        font = cv2.FONT_HERSHEY_SIMPLEX

        x_offset_emotion = 20
        y_offset_emotion = 40
        x_offset = 30
        y_offset = 60

        if type(image_path) is str:
            frame = cv2.imread(image_path)
        else:
            frame=image_path

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = self.fd.process(frame)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        for (x,y,w,h) in faces:
            face = frame[(y - y_offset):(y + h + y_offset),
                        (x - x_offset):(x + w + x_offset)]

            gray_face = gray[(y - y_offset_emotion):(y + h + y_offset_emotion),
                            (x - x_offset_emotion):(x + w + x_offset_emotion)]
            try:
                face = cv2.resize(face, (48, 48))
                gray_face = cv2.resize(gray_face, (48, 48))
            except:
                continue
            face = np.expand_dims(face, 0)
            face = preprocess_input(face)
            gender_label_arg = np.argmax(self.gender_classifier.predict(face))
            gender = gender_labels[gender_label_arg]

            gray_face = preprocess_input(gray_face)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_label_arg = np.argmax(self.emotion_classifier.predict(gray_face))
            emotion = emotion_labels[emotion_label_arg]

            if gender == gender_labels[0]:
                gender_color = (0, 0, 255)
            else:
                gender_color = (255, 0, 0)

            cv2.rectangle(frame, (x, y), (x + w, y + h), gender_color, 2)
            cv2.putText(frame, emotion, (x, y - 40), font,
                            0.5, gender_color, 2, cv2.LINE_AA)
            cv2.putText(frame, gender, (x , y - 40 + 20), font,
                            0.5, gender_color, 2, cv2.LINE_AA)

        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        # cv2.imwrite('predicted_test_image.png', frame)
        cv2.imshow('predicted test image',frame)
        cv2.waitKey(waitTime)
Пример #3
0
def main(template, search):
    print(template.shape, search.shape)
    template = utils.preprocess_input(template)
    search = utils.preprocess_input(search)
    print(template.shape, search.shape)
    model = keras.models.load_model('weights.021.h5', {
        'DepthwiseConv2D': DepthwiseConv2D,
        'Reshape': Reshape
    },
                                    compile=False)
    masks, scores = model.predict([template, search])
    np.savez('result.npz', masks=masks, scores=scores)
Пример #4
0
    def __getitem__(self, idx):
        i = idx * batch_size

        out_img_rows, out_img_cols = img_size * self.scale, img_size * self.scale

        length = min(batch_size, (len(self.names) - i))
        batch_x = np.empty((length, img_size, img_size, channel), dtype=np.float32)
        batch_y = np.empty((length, out_img_rows, out_img_cols, channel), dtype=np.float32)

        for i_batch in range(length):
            name = self.names[i + i_batch]
            filename = os.path.join(image_folder, name)
            # b: 0 <=b<=255, g: 0 <=g<=255, r: 0 <=r<=255.
            image_bgr = cv.imread(filename)

            gt = random_crop(image_bgr, self.scale)

            if np.random.random_sample() > 0.5:
                gt = np.fliplr(gt)

            angle = random.choice((0, 90, 180, 270))
            gt = imutils.rotate_bound(gt, angle)

            x = cv.resize(gt, (img_size, img_size), cv.INTER_CUBIC)

            batch_x[i_batch, :, :] = preprocess_input(x)
            batch_y[i_batch, :, :] = gt

        return batch_x, batch_y
    def do_inference(self, img):

        # image size
        ih, iw, _ = img.shape

        # preprocess input image
        img_rgb = img[:, :, ::-1]
        image_data = utils.preprocess_input(img_rgb, net_w, net_h)

        start_time = time.time()
        # prediction
        out = self.model.predict(image_data)
        print("---cnn inference time: {} seconds ---".format(
            (time.time() - start_time)))
        out[0] = np.squeeze(out[0])
        out[1] = np.squeeze(out[1])
        out[2] = np.squeeze(out[2])

        boxes = list()
        # for i in range(2): #tiny
        for i in range(3):
            # decode the output of the network
            boxes += utils.decode_netout(out[i], anchors[i], obj_threshold,
                                         416, 416)

        boxes = utils.correct_yolo_boxes(boxes, ih, iw, net_w, net_h)

        boxes = utils.do_nms(boxes, nms_threshold)

        # draw boxes onto image
        self.draw_boxes(boxes, img)

        return img, boxes
def run_emotion_net(video_face_imgs, video_preds_queue):

    from keras.models import load_model

    emotion_model = load_model('./fer2013_mini_XCEPTION.119-0.65.hdf5')
    emotion_target_size = emotion_model.input_shape[1:3]

    video_frame_preds = []

    for frame_imgs in video_face_imgs:

        print('lol2')

        face_img_list = []

        for face_img in frame_imgs:

            face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
            face_img = cv2.resize(face_img, (emotion_target_size))
            face_img = preprocess_input(face_img)
            face_img_list.append(face_img)

        face_img_list = np.array(face_img_list)
        face_img_list = face_img_list.reshape(-1, 48, 48, 1)

        frame_preds = emotion_model.predict(face_img_list)

        video_frame_preds.append(frame_preds)

    video_preds_queue.put(video_frame_preds)
Пример #7
0
def predict():
    resp = request.json
    x, img = utils.preprocess_input(resp['data'], resp['size'], n_h=30)
    print('data has been preprocessed')
    rfr_pred = load_models.rfr_300.predict(x)[0]
    print('rf pred: {}'.format(rfr_pred))
    xgb_pred = load_models.xgb_300.predict(x)[0]
    print('xgb_pred: {}'.format(xgb_pred))
    gpr_pred, gpr_var = load_models.gpr_300.predict(x)
    gpr_pred = gpr_pred[0][0] + load_models.gpr_300_shift
    gpr_std = gpr_var[0][0]
    print('gpr_pred: {}'.format(gpr_pred))
    cnn_pred = load_models.cnn_300.predict(img)[0][0]
    print('cnn_pred: {}'.format(cnn_pred))
    response = app.response_class(
        response=json.dumps({
            'rf': str(round(rfr_pred, 1)),
            'xgb': str(round(xgb_pred, 1)),
            'gp': str(round(gpr_pred, 1)),
            'gp_std': str(round(np.sqrt(gpr_std), 1)),
            'cnn': str(round(cnn_pred, 1)),
            #'cnn': 'tbd',
        }),
        status=200,
        mimetype='application/json')
    return response
Пример #8
0
    def __getitem__(self, idx):
        i = idx * batch_size

        length = min(batch_size, (len(self.samples) - i))
        X = np.empty((length, img_rows, img_cols, 3), dtype=np.float32)
        Y = np.zeros((length, img_rows, img_cols, num_classes), dtype=np.float32)

        for i_batch in range(length):
            sample = self.samples[i + i_batch]
            original_image_path = sample['original_image']
            label_image_path = sample['label_image']
            original_image = cv.imread(original_image_path)
            original_image = cv.resize(original_image, (img_cols, img_rows), cv.INTER_NEAREST)
            label_image = cv.imread(label_image_path, 0)
            label_image = cv.resize(label_image, (img_cols, img_rows), cv.INTER_NEAREST)

            X[i_batch] = original_image
            for j in range(num_classes):
                Y[i_batch][label_image == gray_values[j]] = to_categorical(j, num_classes)

        if self.usage == 'train':
            X = seq_img.augment_images(X)
            X = seq_det.augment_images(X)
            Y = seq_det.augment_images(Y)

        X = preprocess_input(X)

        return X, Y
Пример #9
0
def get_prediction():
    # Works only for a single sample
    if request.method == 'POST':
        data = request.data
        data = np.array(preprocess_input(read_image(
            BytesIO(data))))[np.newaxis, ...]
        prediction = model.predict(data).ravel().tolist()
    return str(prediction[0])
Пример #10
0
 def __call__(self, inputs):
     # "inputs" is an image with float values between and 1
     inputs = inputs * 255
     preprocessed_input = preprocess_input(inputs)
     content_outputs = self.vgg(
         preprocessed_input)  # content features of the image
     content_dict = {self.content_layers[0]: content_outputs}
     return content_dict
Пример #11
0
def evaluate(split):
    print('Loading {} set...'.format(split))
    x, y_true = load_data(split)
    x = preprocess_input(x)
    y_pred = model.predict(x)
    y_pred = y_pred.argmax(axis=1)
    print("{} set statistics:".format(split))
    print("Top-1-accuracy: {:.4f}".format(np.mean(y_true == y_pred)))
    print(metrics.classification_report(y_true, y_pred))
Пример #12
0
def preprocess_image(image):
    resized_image = cv2.resize(image,(168,224))
    image_padded = cv2.copyMakeBorder( resized_image, 0, 0, 28, 28, cv2.BORDER_CONSTANT)
    _input = img_to_array(image_padded)
    _input = np.expand_dims(_input, axis=0)
    _input = utils.preprocess_input(_input, version=2)
    _input = np.squeeze(_input)

    return _input
Пример #13
0
    def get_embedding(self, img):  # img: (224, 224, 3)
        # Preprocess
        # img = cv2.resize(img, (224, 224))
        x = img_to_array(img, dtype=np.float32)
        x = x.reshape((1, 224, 224, 3))
        x = preprocess_input(x, version=self.preprocess_version)
        # Get bottleneck features
        embedding = self.model.predict(x).reshape(-1, )

        return embedding
Пример #14
0
def get_frame_data(frames):
    K_plus_frames = select_random_frames(frames)
        
    K_plus_ldmks = [plot_landmarks(f) for f in K_plus_frames]
           
    tx, ty = np.float32(K_plus_frames.pop()), np.float32(K_plus_ldmks.pop())
    x, y = np.float32(K_plus_frames), np.float32(K_plus_ldmks)
    
    tx = preprocess_input(tx, mode='tf')
    ty = preprocess_input(ty, mode='tf')
    x = preprocess_input(x, mode='tf')
    y = preprocess_input(y, mode='tf')
    
    tx = np.expand_dims(tx, axis=0) if len(tx.shape) != 4 else tx
    ty = np.expand_dims(ty, axis=0) if len(ty.shape) != 4 else ty
    
    x = np.expand_dims(x, axis=0) if len(x.shape) != 4 else x
    y = np.expand_dims(y, axis=0) if len(y.shape) != 4 else y
    
    return x, y, tx, ty
Пример #15
0
    def img_to_encoding(self, image_path):
        # print('encoding: ', image_path)
        # if self.pretrained_model is None:
        #     self.pretrained_model = self.load_pretrained_model()

        image = cv2.imread(image_path, 1)
        img = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
        input = img_to_array(img)
        # input = np.expand_dims(input, axis=0)
        input = preprocess_input(input)
        return input
Пример #16
0
def get_embeddings(pixels,model):
	# extract faces
	faces = extract_face(pixels)
	# convert into an array of samples
	samples = asarray(faces, 'float32')
	# prepare the face for the model, e.g. center pixels
	samples = preprocess_input(samples, version=2)
	# create a vggface model
	samples = np.expand_dims(samples,axis = 0)
  # perform prediction
	yhat = model.predict(samples)
	return yhat
Пример #17
0
 def __call__(self, inputs):
     # "inputs" is an image with float values between and 1
     inputs = inputs * 255
     preprocessed_input = preprocess_input(inputs)
     style_outputs = self.vgg(
         preprocessed_input)  # style features of the image
     style_outputs = [gram(s) for s in style_outputs]
     style_dict = {
         layer: out
         for layer, out in zip(self.style_layers, style_outputs)
     }
     return style_dict
def get_features(img):
    '''
    Extract the VGG features (AVG Pooling of the last convolution layer in VGG Face Network).
    :param img: The input to the VGG networks
    :return: VGG features extracted from that image, shape (1,512).
    '''
    sample = convert_img(img)
    sample = np.expand_dims(sample, axis=0)
    sample = preprocess_input(sample, version=1)

    # Return Convolution Features
    return model_conv.predict(sample)
Пример #19
0
def get_data(data_path_file):
    with open(data_path_file,'r',encoding='utf-8') as f:
        data = f.readlines()

    content=[]
    label=[]
    for d in data:
        tmp = d.split('\t')
        im = Image.open(tmp[0])
        x = preprocess_input(np.array(im,dtype='float32'))
        content.append(x)
        label.append(int(tmp[-1].strip().strip('\n'))-1)
    return np.array(content), label
Пример #20
0
def get_embeddings(filenames):
    # extract faces
    faces = [extract_face(f) for f in filenames]
    # convert into an array of samples
    samples = np.asarray(faces, 'float32')
    # prepare the face for the model, e.g. center pixels
    samples = preprocess_input(samples, version=2)

    model = VGGFace(model='resnet50',
                    include_top=False,
                    input_shape=(224, 224, 3),
                    pooling='avg')
    pred = model.predict(samples)
    return pred
Пример #21
0
    def generator():            
        for vid_id in indexes:            
            
            data = pkl.load(open(files[vid_id], 'rb'))
            
            if shuffle_frames:
                random.shuffle(data)

            K_plus_frames = []
            K_plus_ldmks = []
            for d in data:
                K_plus_frames.append(d['frame'])
                K_plus_ldmks.append(d['landmarks'])

            tx, ty = np.float32(K_plus_frames.pop()), np.float32(K_plus_ldmks.pop())
            x, y = np.float32(K_plus_frames), np.float32(K_plus_ldmks)
            
            tx = preprocess_input(tx, mode='tf')
            ty = preprocess_input(ty, mode='tf')
            x = preprocess_input(x, mode='tf')
            y = preprocess_input(y, mode='tf')
            
            yield np.int32(np.array(vid_id).reshape(-1)), np.float32(x), np.float32(y), np.float32(tx), np.float32(ty)
Пример #22
0
    def clustering_metrics(self, n_runs=10, compare_node_types=True):
        loader = self.trainvalidtest_dataloader()
        X_all, y_all, _ = next(iter(loader))
        self.cpu().forward(preprocess_input(X_all, device="cpu"))

        if not isinstance(self._embeddings, dict):
            self._embeddings = {
                list(self._node_ids.keys())[0]: self._embeddings
            }

        embeddings_all, types_all, y_true = self.dataset.get_embeddings_labels(
            self._embeddings, self._node_ids)

        # Record metrics for each run in a list of dict's
        res = [
            {},
        ] * n_runs
        for i in range(n_runs):
            y_pred = self.dataset.predict_cluster(n_clusters=len(
                y_true.unique()),
                                                  seed=i)

            if compare_node_types and len(self.dataset.node_types) > 1:
                res[i].update(
                    clustering_metrics(
                        y_true=types_all,
                        # Match y_pred to type_all's index
                        y_pred=types_all.index.map(
                            lambda idx: y_pred.get(idx, "")),
                        metrics=[
                            "homogeneity_ntype", "completeness_ntype",
                            "nmi_ntype"
                        ]))

            if y_pred.shape[0] != y_true.shape[0]:
                y_pred = y_pred.loc[y_true.index]
            res[i].update(
                clustering_metrics(
                    y_true,
                    y_pred,
                    metrics=["homogeneity", "completeness", "nmi"]))

        res_df = pd.DataFrame(res)
        metrics = res_df.mean(0).to_dict()
        return metrics
Пример #23
0
def pred_default():
    resp = request.json
    x, img = utils.preprocess_input(resp['data'], resp['size'], n_h=30)
    rfr_pred = load_models.rfr_300.predict(x)[0]
    xgb_pred = load_models.xgb_300.predict(x)[0]
    gpr_pred, gpr_var = load_models.gpr_300.predict(x)
    gpr_pred = gpr_pred[0][0] + load_models.gpr_300_shift
    cnn_pred = load_models.cnn_300.predict(img)[0][0]
    response = app.response_class(
        response=json.dumps({
            'rf': str(rfr_pred)[:2],
            'xgb': str(xgb_pred)[:2],
            'gp': str(gpr_pred)[:2],
            'cnn': str(cnn_pred)[:2],
            #'cnn': 'tbd',
        }),
        status=200,
        mimetype='application/json')
    return response
Пример #24
0
def load_data():
    x = []
    y_a = []
    y_g = []
    y_r = []

    # loop the images
    root_path, dirs, files = next(os.walk(train_data_path))

    for f in files:
        f_items = str(f).split('_')
        # age between 1 and 93
        if len(f_items) == 4 and int(f_items[0]) <= 93:
            image = cv2.imread(os.path.join(root_path, f))
            image = cv2.resize(image, (200, 200))
            x.append(image)
            y_a.append(int(f_items[0]) - 1)
            y_g.append(int(f_items[1]))
            y_r.append(int(f_items[2]))

    y_a = utils.to_categorical(y_a, 93)
    y_g = utils.to_categorical(y_g, 2)
    y_r = utils.to_categorical(y_r, 5)

    x = np.array(x)
    y_a = np.array(y_a)
    y_g = np.array(y_g)
    y_r = np.array(y_r)

    # shuffle the indexs
    indexs = np.arange(len(x))
    np.random.shuffle(indexs)

    x = x[indexs]
    y_a = y_a[indexs]
    y_g = y_g[indexs]
    y_r = y_r[indexs]

    # preprocess
    x = my_utils.preprocess_input(x, data_format='channels_last', version=2)
    return x, y_a, y_g, y_r
Пример #25
0
def general_predict(imggray, imgcolor):
    gray_image = np.expand_dims(imggray, axis=2)  #224*224*1
    faces = face_detection.detectMultiScale(imggray, 1.3, 5)
    res = []
    if len(faces) == 0:
        print('No face')
        return None
    else:
        for face_coordinates in faces:
            x1, y1, width, height = face_coordinates
            x1, y1, x2, y2 = x1, y1, x1 + width, y1 + height
            gray_face = gray_image[y1:y2, x1:x2]
            try:
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
                continue
            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            #emotion_probability = np.max(emotion_prediction)
            emotion_label_arg = np.argmax(emotion_prediction)
            res.append([emotion_label_arg, x1, y1, x2, y2])
    '''
    faces = detecttwo.detect_o(imggray)
    res = []
    if len(faces)==0:
        print('No face')
        return None
    else:
        for i in range(len(faces)):
            img,points,fp = detecttwo.detect_o_o_o(detecttwo.detect_o_o(faces[i],imggray))
            img = cv2.resize(img, (48,48), interpolation=cv2.INTER_LINEAR)
            img = np.expand_dims(img,axis=2)#224*224*1
            img = np.array([img])
            img = preprocess_input(img)
            label = np.argmax(emotion_classifier.predict(img),axis=1)[0]
            lx,ly,rx,ry = fp[0][0],fp[0][1],fp[1][0],fp[1][1]
            res.append([label,lx,ly,rx,ry])
    '''
    return res
Пример #26
0
    def __getitem__(self, idx):
        i = idx * batch_size

        length = min(batch_size, (len(self.names) - i))
        batch_x = np.empty((length, img_size, img_size, channel),
                           dtype=np.float32)
        batch_y_x2 = np.empty((length, img_size * 2, img_size * 2, channel),
                              dtype=np.float32)
        batch_y_x3 = np.empty((length, img_size * 3, img_size * 3, channel),
                              dtype=np.float32)
        batch_y_x4 = np.empty((length, img_size * 4, img_size * 4, channel),
                              dtype=np.float32)

        for i_batch in range(length):
            name = self.names[i + i_batch]
            filename = os.path.join(image_folder, name)
            # b: 0 <=b<=255, g: 0 <=g<=255, r: 0 <=r<=255.
            image_bgr = cv.imread(filename)

            gt = random_crop(image_bgr, max_scale)

            if np.random.random_sample() > 0.5:
                gt = np.fliplr(gt)

            angle = random.choice((0, 90, 180, 270))
            gt = imutils.rotate_bound(gt, angle)

            x = cv.resize(gt, (img_size, img_size), cv.INTER_CUBIC)

            gt_x2 = cv.resize(gt, (img_size * 2, img_size * 2), cv.INTER_CUBIC)
            gt_x3 = cv.resize(gt, (img_size * 3, img_size * 3), cv.INTER_CUBIC)
            gt_x4 = gt

            batch_x[i_batch, :, :] = preprocess_input(x)
            batch_y_x2[i_batch, :, :] = gt_x2
            batch_y_x3[i_batch, :, :] = gt_x3
            batch_y_x4[i_batch, :, :] = gt_x4

        return batch_x, [batch_y_x2, batch_y_x3, batch_y_x4]
    def DoTest(self):
        self.train_model.load_weights("epoch_9.h5", by_name=True)

        obj_thresh, nms_thresh = 0.5, 0.45
        print(self.train_ints[0])

        import time
        for i in range(20):
            img_name = '/home/heecheol/Dataset/KNU_Campus/20180312_171706/20180312_171706_'
            num = str(i)
            while len(num) < 4:
                num = '0' + num

            #image = cv2.imread(self.valid_ints[i]['filename'])

            image = cv2.imread(img_name + num + '.jpg')
            image_h, image_w, _ = image.shape

            new_image = utils.preprocess_input(image, 416, 416)
            new_image = np.expand_dims(new_image, 0)

            start_time = time.time()
            pred = self.infer_model.predict(new_image)
            print(time.time() - start_time)
            boxes = []

            boxes += utils.decode_netout(pred[0], self.anchors, obj_thresh,
                                         nms_thresh, 832, 832)
            # correct the sizes of the bounding boxes
            utils.correct_yolo_boxes(boxes, image_h, image_w, 832, 832)

            # suppress non-maximal boxes
            utils.do_nms(boxes, nms_thresh)

            # draw bounding boxes on the image using labels
            utils.draw_boxes(image, boxes, self.labels, obj_thresh)
            cv2.imwrite('vali_img_' + str(i) + '.jpg', image)
Пример #28
0
                tempim = np.zeros((imsize[0], imsize[0], 3), dtype='uint8')
                distant = (imsize[0] - imsize[1]) // 2
                tempim[:, distant:distant + imsize[1], :] = image
                image = tempim
                h = imsize[0]
                w = imsize[0]

            elif imsize[1] > imsize[0]:
                tempim = np.zeros((imsize[1], imsize[1], 3), dtype='uint8')
                distant = (imsize[1] - imsize[0]) // 2
                tempim[distant:distant + imsize[0], :, :] = image
                image = tempim
                h = imsize[1]
                w = imsize[1]

        new_image = preprocess_input(image, 416, 416)

        yolos = infer_model.predict(new_image)
        boxes = []

        for i in range(len(yolos)):
            # decode the output of the network
            boxes += decode_netoutv3(yolos[i][0], config['model']['anchors'],
                                     0.5, 416, 416)

        # correct the sizes of the bounding boxes
        correct_yolo_boxes(boxes, 416, 416, 416, 416)

        # suppress non-maximal boxes
        do_nms(boxes, 0.45)
Пример #29
0
    log['tv_loss'] = []

    # Strip the extension if there is one
    checkpoint_path = os.path.splitext(args.checkpoint_path)[0]

    start_time = time.time()
    # for it in range(args.num_iterations):
    for it in range(args.num_iterations):
        if batch_idx >= batches_per_epoch:
            print('Epoch done. Going back to the beginning...')
            batch_idx = 0

        # Get the batch
        idx = args.batch_size * batch_idx
        batch = X[idx:idx + args.batch_size]
        batch = preprocess_input(batch)
        batch_idx += 1

        # Get class information for each image on the batch
        batch_classes = np.random.randint(args.nb_classes,
                                          size=(args.batch_size, ))

        batch_targets = [Y[l][batch_classes] for l in args.style_layers]

        # Do a step
        start_time2 = time.time()
        out = f_train([batch, batch_classes] + batch_targets + [1.])
        stop_time2 = time.time()
        # Log the statistics

        log['total_loss'].append(out[0])
Пример #30
0
if ROOT_DIR.endswith('examples'):
    ROOT_DIR = os.path.dirname(ROOT_DIR)
TENSORBOARD_DIR = os.path.join(ROOT_DIR, 'tensorboard_logs')
CHECKPOINT_DIR = os.path.join(ROOT_DIR, 'checkpoint')
WEIGHTS_DIR = os.path.join(ROOT_DIR, 'model_weights')
JSON_DIR = os.path.join(ROOT_DIR, 'json_files')

# set up data
x_val, y_val = load_data("val")
x_train, y_train = load_data("train")

num_classes = 12
y_val = keras.utils.to_categorical(y_val, num_classes)
y_train = keras.utils.to_categorical(y_train, num_classes)

x_train = preprocess_input(x_train)
x_val = preprocess_input(x_val)

# set up model
if model_type == 'deepyeast':
    model = DeepYeast()
if opt == 'adam':
    optimizer = keras.optimizers.Adam(lr=lr)
elif opt == 'sgd':
    optimizer = keras.optimizers.SGD(lr=lr, momentum=0.9, nesterov=True)

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=optimizer,
              metrics=['accuracy'])

filepath = os.path.join(
 def preprocess_images(self, image_array):
     return preprocess_input(image_array)
Пример #32
0
        test_dir,
        'P0089_MacularCube512x128_4-25-2013_9-32-13_OD_sn2218_cube_z.img')
    test_images = [
        os.path.join(test_dir, f) for f in os.listdir(test_dir)
        if f.lower().endswith('.bmp')
    ]
    samples = random.sample(test_images, 10)

    for i, filename in enumerate(samples):
        print('Start processing image: {}'.format(filename))

        image = cv.imread(filename)
        image = cv.resize(image, (img_cols, img_rows), cv.INTER_CUBIC)

        x_test = np.empty((1, img_rows, img_cols, 3), dtype=np.float32)
        x_test[0] = preprocess_input(image)

        out = model.predict(x_test)
        out = np.reshape(out, (img_rows, img_cols, num_classes))
        out = np.argmax(out, axis=2)
        for j in range(num_classes):
            out[out == j] = gray_values[j]
        out = out.astype(np.uint8)

        if not os.path.exists('images'):
            os.makedirs('images')

        cv.imwrite('images/{}_image.png'.format(i), image)
        cv.imwrite('images/{}_out.png'.format(i), out)

    K.clear_session()
from models import simple_CNN
from utils import preprocess_input

# parameters
batch_size = 128
num_epochs = 1000
training_split = .8
dataset_name = 'fer2013'
log_file_path = 'log_files/emotion_training.log'
trained_models_path = '../trained_models/emotion_models/simple_CNN'

# data loader
data_loader = DataLoader(dataset_name)
faces, emotions = data_loader.get_data()
print(len(faces))
faces = preprocess_input(faces)
num_classes = emotions.shape[1]
input_shape = faces.shape[1:]

# model parameters/compilation
model = simple_CNN(input_shape, num_classes)
model.compile(optimizer='adam', loss='categorical_crossentropy',
                                        metrics=['accuracy'])
model.summary()

# model callbacks
csv_logger = CSVLogger(log_file_path, append=False)
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5'
model_checkpoint = ModelCheckpoint(model_names,
                            'val_acc', verbose=1,
                                save_best_only=True)