def transform(self, X: dt.Frame): if not os.path.exists(self.model_path): with open(self.model_path, 'wb') as f: f.write(self.model_bytes) import h2oaicore.keras as keras from h2oaicore.models import TensorFlowModel self.tf_config = TensorFlowModel.ConfigProto() # self.tf_config.gpu_options.allow_growth = True self.tf_config.gpu_options.per_process_gpu_memory_fraction = 0.3 keras.backend.set_session( session=TensorFlowModel.make_sess(self.tf_config)) # importlib.reload(keras) self.model = keras.models.load_model(self.model_path) # remove(self.model_path) # can't remove, used by other procs or later values = X[:, self.col_name].to_numpy().ravel() self.batch_size = min(len(values), self.batch_size) values_ = np.array_split(values, int(len(values) / self.batch_size) + 1) print(values_) results = [] for v in values_: images = [] for x in v: if True or x[-4:] in [".jpg", ".png", ".jpeg"]: image = self.preprocess_image(x) images.append(image) else: raise NotImplementedError # deal with missing images (None in images) good_imagei = None for imagei, image in enumerate(images): if image is not None: good_imagei = imagei break if len(images) > 0: msg = "no good images out of %d images" % len(images) if False: assert good_imagei is not None, msg elif good_imagei is None: pass # print_debug(msg) if good_imagei is not None: for imagei, image in enumerate(images): if image is None: images[imagei] = images[ good_imagei] * 0 # impute 0 for missing images images = np.vstack(images) results.append(self.model.predict(images)) if len(results) > 0: return dt.Frame(np.vstack(results)) else: return dt.Frame([0] * X.shape[0])
def __init__(self, batch_size=32, **kwargs): TensorFlowModel.__init__(self, **kwargs) super().__init__(**kwargs) self.batch_size = batch_size self.model_name = "resnet_keras.h5p" self.uuid = "%s-img-data-" % self.__class__.__name__ + self.model_name # + str(uuid.uuid4())[:6] # no, keeps changing and re-loadeing every init self.uuid_tmp = str(uuid.uuid4())[:6] self.col_name = self.input_feature_names[0] self.model_path = os.path.join(user_dir(), self.uuid + ".model") self.model_tmp_path = self.model_path + "_" + self.uuid_tmp + ".tmp" if not os.path.exists(self.model_path): self.download( url= "http://s3.amazonaws.com/artifacts.h2o.ai/releases/ai/h2o/recipes/transformers/img/%s" % self.model_name, dest=self.model_path) with open(self.model_path, 'rb') as f: self.model_bytes = f.read()