示例#1
0
    def load_images(self,dataframe):
        if dataframe is None:
            return None
        else:
            assert type(dataframe) == pd.core.frame.DataFrame, "argument to load image should be dataframe"
            assert  "file_location" in dataframe.columns, "dataframe should contain file_location column"
            output_images = np.zeros((len(dataframe),self.config.image_shape[0],self.config.image_shape[1],self.config.image_shape[2]))
            for index,row in dataframe.iterrows():
                img = cv2.imread(os.path.join(self.config.dataset_dir,row["file_location"].str_replace("mtk", "samuel")))

                if img is None:
                    Log.WARNING("Unable to read images from "+os.path.join(self.config.dataset_dir,row["file_location"].str_replace("mtk", "samuel")))
                    continue
                faces = self.detector(img)
                for i in range(len(faces)):
                    if(len(faces)>0):
                        face_location = faces[i]
                        face_image = img[face_location.top():face_location.bottom(),face_location.left():face_location.right()]
                        try:
                            face_image = cv2.cvtColor(face_image,cv2.COLOR_BGR2GRAY)
                            face_image = cv2.resize(face_image,(self.config.image_shape[0],self.config.image_shape[1]))
                            output_images[index] = face_image.reshape(self.config.image_shape)
                            break
                        except:
                            # Log.ERROR_OUT = True
                            # Log.ERROR ("error"+","+str(face_image is None)+","+str(img is None)+","+ str(len(faces)))
                            # Log.ERROR (str(face_location.top())+","+ str(face_location.bottom())+","+str(face_location.left())+","+str(face_location.right()))
                            print (str(face_location.top())+","+ str(face_location.bottom())+","+str(face_location.left())+","+str(face_location.right()))
                            print ("error"+","+str(face_image is None)+","+str(img is None)+","+ str(len(faces)))
                    else:
                        face_image = cv2.cvtColor(face_image,cv2.COLOR_BGR2GRAY)
                        face_image = cv2.resize(img,(self.config.image_shape[0],self.config.image_shape[1]))
                        output_images[index] = face_image.reshape(self.config.image_shape)
                        Log.WARNING("Dlib unable to find faces from :"+os.path.join(self.config.dataset_dir,row["file_location"].str_replace("mtk", "samuel"))+" Loading full image as face")
            return output_images
示例#2
0
    def load_dataset(self):
        if self.config.label == "detection":
            if not self.contain_dataset_files():
                self.meet_convention()
            Log.DEBUG_OUT = True
            Log.DEBUG("Loading pickle files")
            Log.DEBUG_OUT = False
            self.train_dataset = self.get_meta(
                os.path.join(self.config.dataset_dir, "train.pkl"))
            self.test_dataset = self.get_meta(
                os.path.join(self.config.dataset_dir, "test.pkl"))
            if os.path.exists(
                    os.path.join(self.config.dataset_dir, "validation.pkl")):
                self.validation_dataset = self.get_meta(
                    os.path.join(self.config.dataset_dir, "validation.pkl"))
            else:
                self.validation_dataset = None
                frameinfo = getframeinfo(currentframe())
                Log.WARNING("Unable to find validation dataset",
                            file_name=__name__,
                            line_number=frameinfo.lineno)
            self.train_dataset = self.fix_labeling_issue(self.train_dataset)
            self.test_dataset = self.fix_labeling_issue(self.test_dataset)
            self.validation_dataset = self.fix_labeling_issue(
                self.validation_dataset)
            Log.DEBUG_OUT = True
            Log.DEBUG("Loaded train, test and validation dataset")
            Log.DEBUG_OUT = False
            test_indexes = np.arange(len(self.test_dataset))
            np.random.shuffle(test_indexes)
            validation_indexes = np.arange(len(self.validation_dataset))
            np.random.shuffle(validation_indexes)

            self.test_dataset = self.test_dataset.iloc[
                test_indexes].reset_index(drop=True)
            self.validation_dataset = self.validation_dataset.iloc[
                validation_indexes].reset_index(drop=True)

            self.test_dataset = self.test_dataset[:1000]
            self.validation_dataset = self.validation_dataset[:100]
            Log.DEBUG_OUT = True
            Log.DEBUG_OUT = False
            self.test_dataset_images = self.load_images(
                self.test_dataset).astype(np.float32) / 255
            Log.DEBUG_OUT = True
            Log.DEBUG("Loading validation images")
            Log.DEBUG_OUT = False
            self.validation_dataset_images = self.load_images(
                self.validation_dataset).astype(np.float32) / 255
            self.test_detection = self.test_dataset["is_face"].as_matrix()
            self.dataset_loaded = True
            Log.DEBUG_OUT = True
            Log.DEBUG("Loaded all dataset and images")
            Log.DEBUG_OUT = False

        else:
            raise NotImplementedError("Not implemented for labels:" +
                                      str(self.labels))
示例#3
0
 def __init__(self,config):
     self.config = config
     self.model = AllInOneModel(self.config.image_shape)
     if(config.model_weight!=None and os.path.exists(config.model_weight)):
         Log.DEBUG_OUT = True
         Log.DEBUG("Loading model weights from '"+config.model_weight+"'")
         try:
             self.model.model.load_weights(config.model_weight)
             Log.DEBUG("Loaded model weights")
         except:
             Log.DEBUG("Unable to load model weight from "+config.model_weight)
         Log.DEBUG_OUT =False
示例#4
0
    def load_images(self, dataframe):
        if dataframe is None:
            return None
        else:
            assert type(
                dataframe
            ) == pd.core.frame.DataFrame, "argument to load image should be dataframe"
            assert "file_location" in dataframe.columns, "dataframe should contain file_location column"
            output_images = np.zeros(
                (len(dataframe), self.config.image_shape[0],
                 self.config.image_shape[1], self.config.image_shape[2]))
            for index, row in dataframe.iterrows():
                img = cv2.imread(
                    os.path.join(self.config.dataset_dir,
                                 row["file_location"][0]))

                if img is None:
                    Log.WARNING("Unable to read images from " + os.path.join(
                        self.config.dataset_dir, row["file_location"][0]))
                    continue
                face_location = row["face_location"][0].astype(int)
                face_image = img[face_location[1]:face_location[3],
                                 face_location[0]:face_location[2]]
                face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)
                face_image = cv2.resize(
                    face_image,
                    (self.config.image_shape[0], self.config.image_shape[1]))
                output_images[index] = face_image.reshape(
                    self.config.image_shape)
            return output_images
示例#5
0
 def __init__(self,config):
     self.config = config
     self.model = AllInOneModel(self.config.image_shape)
     self.model.save_model_to_json("/home/samuel/projects/All-In-One/AgeModel.json")
     if(config.model_weight!=None and os.path.exists(config.model_weight)):
         Log.DEBUG_OUT = True
         Log.DEBUG("Loading model weights from '" + config.model_weight +"'")
         try:
             self.model.model.load_weights(config.model_weight)
             #Freeze some layers in the network
             """
             for layer in model.layers[:5]:
                 layers.trainable = False
                 """
             Log.DEBUG("Loaded model weights")
         except:
             Log.DEBUG("Unable to load model weight from "+config.model_weight)
         Log.DEBUG_OUT =False
示例#6
0
    def load_dataset(self):
        if set(self.labels).issubset(["Age", "Gender"]):
            if not self.contain_dataset_files():
                self.meet_convention()
            Log.DEBUG_OUT = True
            Log.DEBUG("Loading pickle files")
            Log.DEBUG_OUT = False
            self.train_dataset = self.get_meta(
                os.path.join(self.config.dataset_dir, "train.pkl"))
            self.test_dataset = self.get_meta(
                os.path.join(self.config.dataset_dir, "test.pkl"))
            if os.path.exists(
                    os.path.join(self.config.dataset_dir, "validation.pkl")):
                self.validation_dataset = self.get_meta(
                    os.path.join(self.config.dataset_dir, "validation.pkl"))
            else:
                self.validation_dataset = None
                frameinfo = getframeinfo(currentframe())
                Log.WARINING_OUT = True
                Log.WARNING("Unable to find validation dataset",
                            file_name=__name__,
                            line_number=frameinfo.lineno)
                Log.WARINING_OUT = False
            self.train_dataset = self.fix_labeling_issue(self.train_dataset)
            self.test_dataset = self.fix_labeling_issue(self.test_dataset)
            self.validation_dataset = self.fix_labeling_issue(
                self.validation_dataset)
            Log.DEBUG_OUT = True
            Log.DEBUG("Loaded train, test and validation dataset")
            Log.DEBUG_OUT = False
            self.test_dataset = self.test_dataset[:5000]
            self.validation_dataset = self.validation_dataset[:100]
            Log.DEBUG_OUT = True
            Log.DEBUG("Loading test images")
            Log.DEBUG_OUT = False
            self.test_dataset_images = self.load_images(
                self.test_dataset).astype(np.float32) / 255
            Log.DEBUG_OUT = True
            Log.DEBUG("Loading validation images")
            Log.DEBUG_OUT = False
            self.validation_dataset_images = self.load_images(
                self.validation_dataset).astype(np.float32) / 255
            self.dataset_loaded = True
            Log.DEBUG_OUT = True
            Log.DEBUG("Loaded all dataset and images")
            Log.DEBUG_OUT = False

        else:
            raise NotImplementedError("Not implemented for labels:" +
                                      str(self.labels))