【Kerasエラー】Your generator is NOT thread-safe.Keras requires a thread-safe generator when`use_multiprocessing=False, workers > 1`.For more information see issue #1638.

環境

  • Ubuntu: 16.04
  • Python: 3.6
  • Tensorflow: 1.9.0
  • Keras: 2.2.0
  • NVIDIA Driver: 410.72
  • CUDA: 10.0

エラー内容

GCPで学習していたら、他のサーバでは出なかったエラーに遭遇した。
Epoch1の重みを保存した後にEpoch2〜38に以下のエラーが一気に出力され、Epoch39からはまた普通に学習し始めた。

Epoch 38/38
Your generator is NOT thread-safe.Keras requires a thread-safe generator when`use_multiprocessing=False, workers > 1`.For more information see issue #1638.

解決方法

fit_generatorの引数にuse_multiprocessing=Trueを追加して解決。

self.model.fit_generator(
                    generator,
                    epochs=self.current_epoch+1,
                    initial_epoch=self.current_epoch,
                    use_multiprocessing=True, # Add here
                    *args, **kwargs
                )

詳しくは以下サイトで。

tech.wonderpla.net

参考

【GCP】ssh_exchange_identification: read: Connection reset by peer

環境

  • クライアント:Mac OS Mojave 10.14.1
  • サーバー:Ubuntu 16.04

エラー

今までは以下のコマンドでGCPに入れたのに、何故か突然エラーを吐き出すようになった。

$ gcloud compute --project "<PROJECT_NAME>" ssh --zone "<REGION_NAME>" "<VM_NAME>" 
ssh_exchange_identification: read: Connection reset by peer
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

原因、解決方法

どうやらサーバー側でアクセスが許可されていなかったことが原因らしいが、なぜ突然アクセス出来なくなったのか…??

$ sudo chmod 777 /etc/hosts.allow
$ vi /etc/hosts.allow # CentOSの場合は/var/log/secure

sshd: ALL # これを追加

エラー「ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory」

問題点

$ python
>>> import tensorflow as tf
Traceback (most recent call last):
  File "/home/saneatsu_wakana/PConv-Keras/venv/lib/python3.5/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/saneatsu_wakana/PConv-Keras/venv/lib/python3.5/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/saneatsu_wakana/PConv-Keras/venv/lib/python3.5/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

原因、解決策

どうやらCUDAやtensorflowのバージョンの関係で起こる問題のようだ。

CUDAのVersion: 10.0、tensorflow==1.8.0 だと問題が起こる。

$ pip uninstall tensorflow
$ pip install tensorflow==1.9.0

$ python3 -c 'import tensorflow as tf; print(tf.__version__)'
1.9.0

バージョンが出るということはimport出来ているということなので無事解決。

他の解決方法としてはCUDA側のバージョンを操作する方法もある。 qiita.com

Seleniumでスクレイピングするときに「is not clickable at point (772, 590). Other element would receive the click:」的なものが出る問題

エラー発生

Seleniumスクレイピングをする際にheadlessのオプションを追加すると以下のようなエラーが出てくる。

product_src_url_and_img_url_hash[:src_url]: https://~~. Error: unknown error: Element <a href="#" id="ms_001" style="outline:none;" title="...(M)">36</a> is not clickable at point (772, 590). Other element would receive the click: <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja" dir="ltr" xmlns:fb="http://ogp.me/ns/fb#" class="mac chrome chrome7 webkit webkit5">...</html>
  (Session info: headless chrome=70.0.3538.77)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.14.0 x86_64).

解決策

結論から言うとウインドウのサイズを指定するオプションを追加すれば良い。

ぐぐってるうちに以下のサイトに遭遇。

tech.medpeer.co.jp

headless chromeは、poltergeistと比べて「ブラウザ上で見える要素であるか否か」にシビアなようです。例えば、position: fixed; left: 1000px としているDOM要素があるとします。このとき、ブラウザのウィンドウサイズが(800, 600)のように小さく要素が画面外になる場合は、その要素は見えないという扱いになります。

ん?単純に見えないならウインドウのサイズを指定してあげたらいいんじゃないか?と思い、以下のようにしてみたら無事解決されました。

  def selenium_and_capybara_config
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--headless')
    options.add_argument('--window-size=1080,1080') # ここを追加する

    Capybara.configure do |capybara_config|
      capybara_config.default_driver = :selenium
      capybara_config.default_max_wait_time = 10
    end

    Capybara.register_driver :selenium do |app|
      Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
    end

    # 今まではoptionsを付けてスクレイピングすると上記エラーが発生していた
    # Selenium::WebDriver.for :chrome 

    Selenium::WebDriver.for :chrome, options: options
  end

無事ブラウザが表示されずにスクレイピングを行うことが出来た。

因みに、 --window-size=800,800 にしてみたところ同じエラーが発生した。
ある程度の大きさが無いとダメっぽい。

【Rails v5.2】bundle install時の「ERROR: While executing gem ... (Errno::EACCES)」を解消

環境

エラー発生

$ bundle install 
:
:
Fetching mysql2 0.4.10
Installing mysql2 0.4.10 with native extensions
Errno::EACCES: Permission denied @ rb_sysopen - /usr/local/var/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10/CHANGELOG.md
An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2

エラー文に書かれているコマンドを実行

$ gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'   
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /usr/local/var/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10/CHANGELOG.md

解決方法

上記のフォルダの権限を変更してもしつこくPermission deniedが出てくるので再帰的にchmodをする。

$ find <FOLDER_NAME> -type f -print | xargs sudo chmod 777       

参考

今回は参考にしなかったけど次はまったときデバッグ方法とか役立ちそう

【keras】weightの中身をみるために「RuntimeError: maximum recursion depth exceeded」と「RecursionError: cannot set the recursion limit the limit is too low」を解決

やりたいこと

画像補完のプログラムを作成しているが、学習によって作成されたweightの中にnanが混ざり込んでいてうまく画像が生成されず真っ黒の画像が出てきてしまう。 そこでweight(h5ファイル)の中身を見てどこがnanになっているか確認したい。

方針1:get_weightsを使う

以下のサイトを参考にした。
kerasのmodel.get_weights()で得られるlistの構造 - Qiita

model = PConvUnet(weight_filepath='data/logs/')
model.get_weights()

ここで以下のエラーが発生

RuntimeError: maximum recursion depth exceeded

再帰処理が最大の深さまでいっちゃてるというエラー。 これはPythonの問題らしいので、深さを増やしてあげると解決できるらしい。

import sys
sys.setrecursionlimit(10000)

すると次は以下のようなエラーが発生

RecursionError: cannot set the recursion limit the limit is too low 〜〜

方針を変える。

方針2:model.layersをforループで回す

for layer in model.layers:
    print(layer)

すると以下のようなエラーが発生。
エラー内容は以下を参考に。

TypeError: 'method' object is not iterable

ここれいうmodelは、自作のPConvUnetというものをラッパーとして使っているので、結局以下のようにしてnanを発見した。

for layer in model.model.layers:
    weights = layer.get_weights()
    for weight in weights:
        print(weight.shape)
        if np.any(np.isnan(weight)):
            print(layer.name)
            print(weights)

参考まとめ

【自分用メモ】gitで色々詰まったときにみる

自分の.gitconfig

github.com

取り消す系

直前のcommitをやり直す

$ git add -A
$ git commit --amend -m "Commit message"

git commitをやり直しする&取り消しする(「git commit --amend」と「git reset」)

addを取り消す

$ git reset HEAD <FILENAME>

git add の取り消し方法と、関連コマンドまとめ

masterへのpushを取り消す

# まずは以下2つのコマンドで特定のコミットまで戻る
$ git log --oneline # ログの番号確認
$ git reset --hard <上で確認した戻りたいcommitの番号>


$ git log --oneline # 目的の箇所まで戻れているか確認
$ git push -f

↓が詳しかった

【git】git pushを取り消す - tweeeetyのぶろぐ的めも

エラー対処

push時にerror: failed to push some refs to

$ git push origin :<BRANCH>
$ git push origin <BRANCH>

:をつけるとremoteのブランチをDeleteするということになる。 これはローカルブランチが絶対に正しいときに使う。

その他の操作

新規ファイルをstashする

# ✕
$ git stash
No local changes to save

# ◯
$ git stash -u

新規フォルダをstashする

# ✕
$ git status
On branch refactor-question-cron
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   db/sql/init.sql

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/


$ git stash -u
$ git status
On branch refactor-question-cron
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/
    # ↑フォルダがstashされてない

# ◯
$ git stash --include-untracked
$ git status
On branch refactor-question-cron
nothing to commit, working tree clean

ブランチにmasterの内容を反映させる

$ git checkout master
$ git pull origin master
$ git checkout <BRANCH_NAME>
$ git rebase <BRANCH_NAME>

Gitで開発ブランチにmasterの内容を反映させる方法 (git rebase)

ブランチを作り忘れた

$ git stash
$ git stash list
$ git checkout -b <BRANCH_NAME>

$ git stash pop # 実行後適用した状態は削除される
$ git stash apply # 削除されない

まだcommitしてない場合は以下でおk。

$ git checkout -b <BRANCH_NAME>

ブランチを作り忘れた時 - Qiita

特定のブランチをクローンする

$ git clone -b ブランチ名 リポジトリのアドレス

deleteしたファイルをstageにaddしたい

$ git add <FILENAME> --update

Git 、削除したファイルが stage に上手く上がらない時 - CHROMA

git logを見やすくしたい

# いい感じのコマンドを「git log」に登録して使えるようにする
$ git config --global alias.tree 'log --graph --all --format="%x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(magenta reverse)%d%Creset %s"'

git log を見やすくする - Qiita

conflictしたファイルの一覧を表示させる

$ git diff --name-only --diff-filter=U

# エイリアスを設定
$ git config --global alias.conf '!git ls-files -u | cut -f 2 | sort -u'

Git で競合ファイル一覧を楽に見る方法 - Qiita

設定したエイリアスを確認する

$ git config --global --list | grep alias\.