Title: Saving and Loading model errors · Issue #25 · pythonlessons/mltu · GitHub
Open Graph Title: Saving and Loading model errors · Issue #25 · pythonlessons/mltu
X Title: Saving and Loading model errors · Issue #25 · pythonlessons/mltu
Description: Hi, I am trying to train my model on my database according to the tutorial and sometimes the training takes quite a long time so I wanted to load the model saved by callback using this code: if os.path.exists("Model/model.h5"): HTR_Model...
Open Graph Description: Hi, I am trying to train my model on my database according to the tutorial and sometimes the training takes quite a long time so I wanted to load the model saved by callback using this code: if os....
X Description: Hi, I am trying to train my model on my database according to the tutorial and sometimes the training takes quite a long time so I wanted to load the model saved by callback using this code: if os....
Opengraph URL: https://github.com/pythonlessons/mltu/issues/25
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Saving and Loading model errors","articleBody":"Hi,\r\n I am trying to train my model on my database according to the tutorial and sometimes the training takes quite a long time so I wanted to load the model saved by callback using this code:\r\n```\r\n if os.path.exists(\"Model/model.h5\"):\r\n HTR_Model = load_model(\"Model/model.h5\")\r\n new_model = False\r\n else:\r\n img_shape = (self.height, self.width, 3)\r\n HTR_Model = self.HTR_Model(img_shape, characters_num, vocab)\r\n HTR_Model.compile_model()\r\n HTR_Model.summary(line_length=110)\r\n new_model = True\r\n```\r\nAnd then continue training with this code:\r\n```\r\n earlystopper = EarlyStopping(monitor='val_CER', patience=20, verbose=1, mode='min')\r\n checkpoint = ModelCheckpoint(\"Model/model.h5\", monitor='val_CER', verbose=1, save_best_only=True, mode='min')\r\n trainLogger = TrainLogger(\"Model\")\r\n tb_callback = TensorBoard('Model/logs', update_freq=1)\r\n reduceLROnPlat = ReduceLROnPlateau(monitor='val_CER', factor=0.9, min_delta=1e-10, patience=10, verbose=1,\r\n mode='auto')\r\n model2onnx = Model2onnx(\"Model/model.h5\")\r\n\r\n if new_model is True:\r\n HTR_Model.train(training_data,\r\n val_data,\r\n epochs=1000,\r\n workers=20,\r\n callbacks=[earlystopper, checkpoint, trainLogger, reduceLROnPlat, tb_callback, model2onnx])\r\n else:\r\n HTR_Model.fit(training_data,\r\n validation_data=val_data,\r\n epochs=1000,\r\n workers=20,\r\n callbacks=[earlystopper, checkpoint, trainLogger, reduceLROnPlat, tb_callback, model2onnx],\r\n )\r\n```\r\n\r\nUnfortunately I encountered the following error:\r\n`ValueError: Unknown loss function: CTCloss. Please ensure this object is passed to the custom_objects argument.`\r\n\r\nSo I tried to add this argument like this:\r\n```\r\nHTR_Model = load_model(\"Model/model.h5\", custom_objects={'CTCloss': CTCloss})\r\n```\r\nBut It didn't work and I got this error:\r\n`TypeError: CTCloss.__init__() got an unexpected keyword argument reduction`\r\n\r\nI couldn't solve it so I started looking for other ways to load the model. This time I tried to do it by saving the file in .tf format and load it without custom_objects argument and it caused an error:\r\n`Unable to restore custom object of type _tf_keras_metric. Please make sure that any custom layers are included in the custom_objects arg when calling load_model() and make sure that all layers implement get_config and from_config.`\r\n\r\nAfter that I added argument like this:\r\n```\r\nHTR_Model = load_model(\"Model/model.tf\", custom_objects={'CERMetric': CERMetric(vocabulary=vocab), 'WERMetric': WERMetric(vocabulary=vocab)})\r\n```\r\nAnd the error was \r\n`TypeError: CERMetric.__init__() missing 1 required positional argument: 'vocabulary'`\r\nEven though I used this argument. The only thing that works is this code:\r\n```\r\nHTR_Model = load_model(\"Model/model.h5\", compile=False)\r\nHTR_Model.compile(loss=CTCloss(), metrics=[CERMetric(vocabulary=vocab), WERMetric(vocabulary=vocab)], run_eagerly=False)\r\n```\r\nBut it doesn't seem to be loading all these weights. I also tried using BackupAndRestore and picked up where I left off but still couldn't see if it saves those weights and continues using them. So Is it possible to somehow load a saved model while training is interrupted and continue training it so that it stays in accordance with the tutorial? (For example, I have epoch 53 /1000 and I see that the best value yet was saved to the model.h5 file at 52 epoch so I stop learning and then I want to load the saved model at epoch 52 and continue from there)","author":{"url":"https://github.com/Pixel535","@type":"Person","name":"Pixel535"},"datePublished":"2023-08-01T18:44:39Z","url":"https://github.com/pythonlessons/mltu/issues/25"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d31d84a4-4a8c-7337-0315-e761c3483e28 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81FA:27A224:3B85E7:505361:6B0438B6 |
| html-safe-nonce | 3bc195eed1475b5b31eae6442d46198c24522eccc0eb31db82b4e03b687e75b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUZBOjI3QTIyNDozQjg1RTc6NTA1MzYxOjZCMDQzOEI2IiwidmlzaXRvcl9pZCI6IjgzNjY3OTc2MDM4MTU1Njk1OTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e4b2770d3f7a2e280f9852408859fd17a179909695c293efa0e09b4aeb6b77ad |
| hovercard-subject-tag | issue:1831832328 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonlessons/mltu/25/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c9387da9540328e805c32df255331435c401ba9149df3ceef4131f834681fb12/pythonlessons/mltu/issues/25 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c9387da9540328e805c32df255331435c401ba9149df3ceef4131f834681fb12/pythonlessons/mltu/issues/25 |
| og:image:alt | Hi, I am trying to train my model on my database according to the tutorial and sometimes the training takes quite a long time so I wanted to load the model saved by callback using this code: if os.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Pixel535 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9185ccfefd80c1ff2d9ddcdb0cd2becf46b5a1efbbf30741a895e4bb8de25e64 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonlessons/mltu git https://github.com/pythonlessons/mltu.git |
| octolytics-dimension-user_id | 43852392 |
| octolytics-dimension-user_login | pythonlessons |
| octolytics-dimension-repository_id | 572996960 |
| octolytics-dimension-repository_nwo | pythonlessons/mltu |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 572996960 |
| octolytics-dimension-repository_network_root_nwo | pythonlessons/mltu |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 06dc448a76fe4751ac1db26524aefa3aa3e889a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width