简体   繁体   English

如何从keras author-A_K_Nain中保存ocr model

[英]how to save ocr model from keras author-A_K_Nain

Im studying tensorflow ocr model from keras example authored by A_K_Nain.我正在研究 tensorflow ocr model 来自 A_K_Nain 编写的 keras 示例。 This model use custom object (CTC Layer).这个 model 使用自定义 object(CTC 层)。 It is in the site: https://keras.io/examples/vision/captcha_ocr/ I trained model using my dataset and then the result of prediction model is perfect.它在站点中: https://keras.io/examples/vision/captcha_ocr/我使用我的数据集训练了 model,然后预测 model 的结果是完美的。 I want to save and load this model and i tried it.我想保存并加载这个 model,我试过了。 But i got some errors so i appended this code in CTC Layer class.但是我遇到了一些错误,所以我在 CTC 层 class 中附加了这段代码。

def get_config(self):
    config = super(CTCLayer, self).get_config()
    config.update({"name":self.name})
    return config

After that I tried to save whole model and weight but nothing worked.之后我试图保存整个 model 和重量,但没有任何效果。 So i applied 2 save point.所以我申请了2个保存点。 First way.第一种方式。

history = model.fit(
    train_dataset,
    validation_data=validation_dataset,
    epochs=70,
    callbacks=[early_stopping],
)

model.save('./model/my_model')

---------------------------------------

new_model = load_model('./model/my_model', custom_objects={'CTCLayer':CTCLayer})

prediction_model = keras.models.Model(
  new_model .get_layer(name='image').input, new_model .get_layer(name='dense2').output
)

and second way.和第二种方式。

prediction_model = keras.models.Model(
  model.get_layer(name='image').input, model.get_layer(name='dense2').output
)

prediction_model.save('./model/my_model')

These still never worked.这些仍然没有用。 it didn't make error but result of prediction is terrible.它没有出错,但预测结果很糟糕。 Accurate results are obtained when training and saving and loading are performed together.当训练和保存和加载一起执行时,可以获得准确的结果。 If I load same model without training together, the result is so bad.如果我在没有一起训练的情况下加载相同的 model,结果会很糟糕。

How can i use this model without training everytime?我如何在每次不训练的情况下使用这个 model? please help me.请帮我。

The problem does not come from tensorflow. In the captcha_ocr tutorial, characters is a set, sets are unordered.问题不是来自tensorflow。在captcha_ocr教程中, characters是一个集合,集合是无序的。 So the mapping from characters to integers using StringLookup is dependent of the current run of the notebook.因此,使用StringLookup从字符到整数的映射取决于笔记本的当前运行 That is why you get rubbish when using it in another notebook without retraining, the mapping is not the same!这就是为什么在没有重新训练的情况下在另一个笔记本上使用它时会出现垃圾的原因,映射不一样!
A solution is to use an ordered list instead of the set for characters :一种解决方案是使用有序列表而不是characters集:

characters = sorted(list(set([char for label in labels for char in label])))

Note that the set operator here permits to get a unique version of each character and then it is converted back to a list and sorted.请注意,此处的set运算符允许获取每个字符的唯一版本,然后将其转换回列表并进行排序。 It will work then on any script/notebook without retraining (using the same formula).然后它可以在任何脚本/笔记本上运行而无需重新训练(使用相同的公式)。

The problem is not in the saved model but in the character list that you are using to map number back to string.问题不在保存的 model 中,而是在您用于将 map 数字返回字符串的字符列表中。 Everytime you restart the notebook, it resets the character list and when you load your model, it can't accurately map the numbers back to string.每次重新启动笔记本时,它都会重置字符列表,当您加载 model 时,它无法准确地将 map 中的数字恢复为字符串。 To resolve this issue you need to save character list.要解决此问题,您需要保存字符列表。 Please follow the below code.请遵循以下代码。

train_labels_cleaned = []
characters = set()
max_len = 0

for label in train_labels:
  label = label.split(" ")[-1].strip()
  for char in label:
    characters.add(char)

  max_len = max(max_len, len(label))
  train_labels_cleaned.append(label)

print("Maximum length: ", max_len)
print("Vocab size: ", len(characters))

# Check some label samples
train_labels_cleaned[:10]

ff = list(characters)

# save list as pickle file
import pickle
with open("/content/drive/MyDrive/Colab Notebooks/OCR_course/characters", "wb") as fp:   #Pickling
    pickle.dump(ff, fp)

# Load character list again
import pickle
with open("/content/drive/MyDrive/Colab Notebooks/OCR_course/characters", "rb") as fp:   # Unpickling
    b = pickle.load(fp)
    print(b)

AUTOTUNE = tf.data.AUTOTUNE

# Maping characaters to integers
char_to_num = StringLookup(vocabulary=b, mask_token=None)

#Maping integers back to original characters
num_to_chars = StringLookup(vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True)

Now when you map back the numbers to string after prediction, it will retain the original order and will predict accurately.现在当你 map 在预测后将数字返回到字符串时,它会保留原来的顺序并且会准确预测。

If you still didn't understand the logic, you can watch my video in which I explained this project from scratch and resolved all the issues that you are facing.如果您仍然不明白其中的逻辑,您可以观看我的视频,我在视频中从头开始解释了这个项目并解决了您面临的所有问题。

https://youtu.be/ZiUEdS_5Byc https://youtu.be/ZiUEdS_5Byc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM