Google Colab - tensowflow object detection api - 'function' object has no attribute 'called' - python-3.x

I encountered the following error when I try to test the object detection api model_builder_test.py.
!apt-get install -y -qq protobuf-compiler python-pil python-lxml
!git clone --quiet https://github.com/tensorflow/models.git
import os
os.chdir('models/research')
!protoc object_detection/protos/*.proto --python_out=.
import sys
sys.path.append('/content/models/research/slim')
%run object_detection/builders/model_builder_test.py
The following error appears after running the model_builder_test.py
.W0220 03:22:35.097244 140099951081344 deprecation.py:323] From
/content/models/research/object_detection/anchor_generators/grid_anchor_generator.py:59:
to_float (from tensorflow.python.ops.math_ops) is deprecated and will
be removed in a future version. Instructions for updating: Use tf.cast
instead. .. WARNING: The TensorFlow contrib module will not be
included in TensorFlow 2.0. For more information, please see: *
https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons If you depend on functionality not listed there, please file an issue.
..................s
---------------------------------------------------------------------- Ran 22 tests in 0.203s
OK (skipped=1)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call
last) in ()
----> 1 get_ipython().magic('run object_detection/builders/model_builder_test.py')
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py
in magic(self, arg_s) 2158 magic_name, _, magic_arg_s =
arg_s.partition(' ') 2159 magic_name =
magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2160 return self.run_line_magic(magic_name, magic_arg_s) 2161 2162
-------------------------------------------------------------------------
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py
in run_line_magic(self, magic_name, line) 2079
kwargs['local_ns'] = sys._getframe(stack_depth).f_locals 2080
with self.builtin_trap:
-> 2081 result = fn(*args,**kwargs) 2082 return result 2083
in run(self, parameter_s, runner, file_finder)
/usr/local/lib/python3.6/dist-packages/IPython/core/magic.py in
(f, *a, **k)
186 # but it's overkill for just that one bit of state.
187 def magic_deco(arg):
--> 188 call = lambda f, *a, **k: f(*a, **k)
189
190 if callable(arg):
/usr/local/lib/python3.6/dist-packages/IPython/core/magics/execution.py
in run(self, parameter_s, runner, file_finder)
740 else:
741 # regular execution
--> 742 run()
743
744 if 'i' in opts:
/usr/local/lib/python3.6/dist-packages/IPython/core/magics/execution.py
in run()
726 def run():
727 runner(filename, prog_ns, prog_ns,
--> 728 exit_ignore=exit_ignore)
729
730 if 't' in opts:
/usr/local/lib/python3.6/dist-packages/IPython/core/pylabtools.py in
mpl_execfile(fname, *where, **kw)
175 matplotlib.interactive(is_interactive)
176 # make rendering call now, if the user tried to do it
--> 177 if plt.draw_if_interactive.called:
178 plt.draw()
179 plt.draw_if_interactive.called = False
AttributeError: 'function' object has no attribute 'called'

This is how I overcame the issue:
install prompt-toolkit to the version 1.0.15, as explained in the link below
https://github.com/jupyter/jupyter_console/issues/158
restart the runtime to activate the package
use '!python' instead of '%run'

Related

Huggingface tokenizer not able to load model after upgrading python to 3.10

I just updated Python to version 3.10.8. Note that I use JupyterLab.
I had to re-install a lot of packages, but now I get an error when I try to load the tokenizer of an HuggingFace model
This is my code:
# Import libraries
from transformers import pipeline, AutoTokenizer
# Define checkpoint
model_checkpoint = 'deepset/xlm-roberta-large-squad2'
# Tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
Note that version of transformers is 4.24.0.
This is the error I get:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [3], line 2
1 # Tokenizer
----> 2 tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
File ~/.local/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py:637, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
635 tokenizer_class_py, tokenizer_class_fast = TOKENIZER_MAPPING[type(config)]
636 if tokenizer_class_fast and (use_fast or tokenizer_class_py is None):
--> 637 return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
638 else:
639 if tokenizer_class_py is not None:
File ~/.local/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1777, in PreTrainedTokenizerBase.from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
1774 else:
1775 logger.info(f"loading file {file_path} from cache at {resolved_vocab_files[file_id]}")
-> 1777 return cls._from_pretrained(
1778 resolved_vocab_files,
1779 pretrained_model_name_or_path,
1780 init_configuration,
1781 *init_inputs,
1782 use_auth_token=use_auth_token,
1783 cache_dir=cache_dir,
1784 local_files_only=local_files_only,
1785 _commit_hash=commit_hash,
1786 **kwargs,
1787 )
File ~/.local/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1932, in PreTrainedTokenizerBase._from_pretrained(cls, resolved_vocab_files, pretrained_model_name_or_path, init_configuration, use_auth_token, cache_dir, local_files_only, _commit_hash, *init_inputs, **kwargs)
1930 # Instantiate tokenizer.
1931 try:
-> 1932 tokenizer = cls(*init_inputs, **init_kwargs)
1933 except OSError:
1934 raise OSError(
1935 "Unable to load vocabulary from file. "
1936 "Please check that the provided vocabulary is accessible and not corrupted."
1937 )
File ~/.local/lib/python3.10/site-packages/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py:155, in XLMRobertaTokenizerFast.__init__(self, vocab_file, tokenizer_file, bos_token, eos_token, sep_token, cls_token, unk_token, pad_token, mask_token, **kwargs)
139 def __init__(
140 self,
141 vocab_file=None,
(...)
151 ):
152 # Mask token behave like a normal word, i.e. include the space before it
153 mask_token = AddedToken(mask_token, lstrip=True, rstrip=False) if isinstance(mask_token, str) else mask_token
--> 155 super().__init__(
156 vocab_file,
157 tokenizer_file=tokenizer_file,
158 bos_token=bos_token,
159 eos_token=eos_token,
160 sep_token=sep_token,
161 cls_token=cls_token,
162 unk_token=unk_token,
163 pad_token=pad_token,
164 mask_token=mask_token,
165 **kwargs,
166 )
168 self.vocab_file = vocab_file
169 self.can_save_slow_tokenizer = False if not self.vocab_file else True
File ~/.local/lib/python3.10/site-packages/transformers/tokenization_utils_fast.py:114, in PreTrainedTokenizerFast.__init__(self, *args, **kwargs)
111 fast_tokenizer = TokenizerFast.from_file(fast_tokenizer_file)
112 elif slow_tokenizer is not None:
113 # We need to convert a slow tokenizer to build the backend
--> 114 fast_tokenizer = convert_slow_tokenizer(slow_tokenizer)
115 elif self.slow_tokenizer_class is not None:
116 # We need to create and convert a slow tokenizer to build the backend
117 slow_tokenizer = self.slow_tokenizer_class(*args, **kwargs)
File ~/.local/lib/python3.10/site-packages/transformers/convert_slow_tokenizer.py:1162, in convert_slow_tokenizer(transformer_tokenizer)
1154 raise ValueError(
1155 f"An instance of tokenizer class {tokenizer_class_name} cannot be converted in a Fast tokenizer instance."
1156 " No converter was found. Currently available slow->fast convertors:"
1157 f" {list(SLOW_TO_FAST_CONVERTERS.keys())}"
1158 )
1160 converter_class = SLOW_TO_FAST_CONVERTERS[tokenizer_class_name]
-> 1162 return converter_class(transformer_tokenizer).converted()
File ~/.local/lib/python3.10/site-packages/transformers/convert_slow_tokenizer.py:438, in SpmConverter.__init__(self, *args)
434 requires_backends(self, "protobuf")
436 super().__init__(*args)
--> 438 from .utils import sentencepiece_model_pb2 as model_pb2
440 m = model_pb2.ModelProto()
441 with open(self.original_tokenizer.vocab_file, "rb") as f:
File ~/.local/lib/python3.10/site-packages/transformers/utils/sentencepiece_model_pb2.py:20
18 from google.protobuf import descriptor as _descriptor
19 from google.protobuf import message as _message
---> 20 from google.protobuf import reflection as _reflection
21 from google.protobuf import symbol_database as _symbol_database
24 # ##protoc_insertion_point(imports)
File /usr/lib/python3/dist-packages/google/protobuf/reflection.py:58
56 from google.protobuf.pyext import cpp_message as message_impl
57 else:
---> 58 from google.protobuf.internal import python_message as message_impl
60 # The type of all Message classes.
61 # Part of the public interface, but normally only used by message factories.
62 GeneratedProtocolMessageType = message_impl.GeneratedProtocolMessageType
File /usr/lib/python3/dist-packages/google/protobuf/internal/python_message.py:69
66 import copyreg as copyreg
68 # We use "as" to avoid name collisions with variables.
---> 69 from google.protobuf.internal import containers
70 from google.protobuf.internal import decoder
71 from google.protobuf.internal import encoder
File /usr/lib/python3/dist-packages/google/protobuf/internal/containers.py:182
177 collections.MutableMapping.register(MutableMapping)
179 else:
180 # In Python 3 we can just use MutableMapping directly, because it defines
181 # __slots__.
--> 182 MutableMapping = collections.MutableMapping
185 class BaseContainer(object):
187 """Base container class."""
AttributeError: module 'collections' has no attribute 'MutableMapping'
I tried several solutions (for example, this and this), but none seem to work.
According to this link, I should change collections.Mapping into collections.abc.Mapping, but I wouldn't knwo where to do it.
Another possible solution is downgrading Python to 3.9, but I would like to keep it as last resort.
How can I fix this?
Turned out it was a problem related to protobuf module. I updated it to the latest version to date (which is 4.21.9).
This changed the error to:
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
So I downgraded protobuf to version 3.20.0 and that worked.
For further details, look here.

Problem loading trained scikit-learn/imblearn pipeline model

I have built, trained an imblearn.pipeline Pipeline with imblearn and RandomForestClassifer from Scikit-learn.
The model is saved using joblib.dump('model.joblib').
However, when I try to load the model, it throws an error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-26-d3ee277020d2> in <module>
----> 1 model = joblib.load('model.joblib')
~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/site-packages/joblib/numpy_pickle.py in load(filename, mmap_mode)
583 return load_compatibility(fobj)
584
--> 585 obj = _unpickle(fobj, filename, mmap_mode)
586 return obj
~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/site-packages/joblib/numpy_pickle.py in _unpickle(fobj, filename, mmap_mode)
502 obj = None
503 try:
--> 504 obj = unpickler.load()
505 if unpickler.compat_mode:
506 warnings.warn("The file '%s' has been generated with a "
~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in load(self)
1086 raise EOFError
1087 assert isinstance(key, bytes_types)
-> 1088 dispatch[key[0]](self)
1089 except _Stop as stopinst:
1090 return stopinst.value
~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in load_global(self)
1374 module = self.readline()[:-1].decode("utf-8")
1375 name = self.readline()[:-1].decode("utf-8")
-> 1376 klass = self.find_class(module, name)
1377 self.append(klass)
1378 dispatch[GLOBAL[0]] = load_global
~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in find_class(self, module, name)
1424 elif module in _compat_pickle.IMPORT_MAPPING:
1425 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1426 __import__(module, level=0)
1427 if self.proto >= 4:
1428 return _getattribute(sys.modules[module], name)[0]
ModuleNotFoundError: No module named 'imblearn.over_sampling._smote.base'; 'imblearn.over_sampling._smote' is not a package
I do have imblearn installed in the conda environment. Not sure why it's not finding imblearn. Any tips will be helpful.
use
python-m pip install package name
to install it globally on system ,maybe it can help

ValueError: Not enough values to unpack in tfds.load

I am new to Python and Tensorflow. While executing the tfds.load function, I got following error. I have spent hours trying to understand the error, but I'm at a loss. Any help would be appreciated.
I am using following versions: python 3.8, tensorflow 2.3 and tensorflow-datasets 1.2
ValueError Traceback (most recent call last)
<ipython-input-2-41baf13b8c3f> in <module>
----> 1 mnistdataset, mnist_info = tfds.load("mnist",
with_info=True, as_supervised=True)
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\api_utils.py in
disallow_positional_args_dec(fn, instance, args, kwargs)
50 _check_no_positional(fn, args, ismethod, allowed=allowed)
51 _check_required(fn, kwargs)
---> 52 return fn(*args, **kwargs)
53
54 return disallow_positional_args_dec(wrapped) # pylint: disable=no-value-for-parameter
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\registered.py in load(name,
split, data_dir, batch_size, in_memory, shuffle_files, download, as_supervised, decoders, with_info,
builder_kwargs, download_and_prepare_kwargs, as_dataset_kwargs, try_gcs)
298 if download:
299 download_and_prepare_kwargs = download_and_prepare_kwargs or {}
--> 300 dbuilder.download_and_prepare(**download_and_prepare_kwargs)
301
302 if as_dataset_kwargs is None:
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\api_utils.py in
disallow_positional_args_dec(fn, instance, args, kwargs)
50 _check_no_positional(fn, args, ismethod, allowed=allowed)
51 _check_required(fn, kwargs)
---> 52 return fn(*args, **kwargs)
53
54 return disallow_positional_args_dec(wrapped) # pylint: disable=no-value-for-parameter
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\dataset_builder.py in
download_and_prepare(self, download_dir, download_config)
260 dl_manager = self._make_download_manager(
261 download_dir=download_dir,
--> 262 download_config=download_config)
263
264 # Currently it's not possible to overwrite the data because it would
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\dataset_builder.py in
_make_download_manager(self, download_dir, download_config)
660 force_download=(download_config.download_mode == FORCE_REDOWNLOAD),
661 force_extraction=(download_config.download_mode == FORCE_REDOWNLOAD),
--> 662 register_checksums=download_config.register_checksums,
663 )
664
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\api_utils.py in
disallow_positional_args_dec(fn, instance, args, kwargs)
50 _check_no_positional(fn, args, ismethod, allowed=allowed)
51 _check_required(fn, kwargs)
---> 52 return fn(*args, **kwargs)
53
54 return disallow_positional_args_dec(wrapped) # pylint: disable=no-value-for-parameter
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\download\download_manager.py in
__init__(self, download_dir, extract_dir, manual_dir, dataset_name, force_download, force_extraction,
register_checksums)
175 self._register_checksums = register_checksums
176 # All known URLs: {url: (size, checksum)}
--> 177 self._sizes_checksums = checksums.get_all_sizes_checksums()
178 # To record what is being used: {url: (size, checksum)}
179 self._recorded_sizes_checksums = {}
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\download\checksums.py in
get_all_sizes_checksums()
127 sizes_checksums = {}
128 for path in _checksum_paths().values():
--> 129 data = _get_sizes_checksums(path)
130 for url, size_checksum in data.items():
131 if (url in sizes_checksums and
~\anaconda3\envs\py3-TF2.0\lib\site-packages\tensorflow_datasets\core\download\checksums.py in
_get_sizes_checksums(checksums_path)
117 continue
118 # URL might have spaces inside, but size and checksum will not.
--> 119 url, size, checksum = line.rsplit(' ', 2)
120 checksums[url] = (int(size), checksum)
121 return checksums
ValueError: not enough values to unpack (expected 3, got 1)
From comments
After upgrading tensorflow-datasets from 1.2 to 4.2, issue was
resolved. (paraphrased from Niteya Shah)
I also was having the issues and this would solve the problems:
pip install tensorflow-datasets=4.3
I had the same problem, my solution, create a new environment just with:
conda create --name py3-TF2.0 python=3
conda activate py3-TF2.0
pip install --upgrade pip
pip install tensorflow
pip install --upgrade tensorflow
pip install tensorflow-datasets
pip install ipykernel

Python3 code Uploading to S3 bucket with IO instead of String IO

I am trying to download the zip file in memory, expand it and upload it to S3.
import boto3
import io
import zipfile
import mimetypes
s3 = boto3.resource('s3')
service_zip = io.BytesIO()
service_bucket = s3.Bucket('services.mydomain.com')
build_bucket = s3.Bucket('servicesbuild.mydomain.com')
build_bucket.download_fileobj('servicesbuild.zip', service_zip)
with zipfile.ZipFile(service_zip) as myzip:
for nm in myzip.namelist():
obj = myzip.open(nm)
print(obj)
service_bucket.upload_fileobj(obj,nm,
ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
service_bucket.Object(nm).Acl().put(ACL='public-read')
Here is the error I get
<zipfile.ZipExtFile name='favicon.ico' mode='r' compress_type=deflate>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5941e5e45adc> in <module>
18 print(obj)
19 service_bucket.upload_fileobj(obj,nm,
---> 20 ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
21 service_bucket.Object(nm).Acl().put(ACL='public-read')
~/bitbucket/clguru/env/lib/python3.7/site-packages/boto3/s3/inject.py in bucket_upload_fileobj(self, Fileobj, Key, ExtraArgs, Callback, Config)
579 return self.meta.client.upload_fileobj(
580 Fileobj=Fileobj, Bucket=self.name, Key=Key, ExtraArgs=ExtraArgs,
--> 581 Callback=Callback, Config=Config)
582
583
~/bitbucket/clguru/env/lib/python3.7/site-packages/boto3/s3/inject.py in upload_fileobj(self, Fileobj, Bucket, Key, ExtraArgs, Callback, Config)
537 fileobj=Fileobj, bucket=Bucket, key=Key,
538 extra_args=ExtraArgs, subscribers=subscribers)
--> 539 return future.result()
540
541
~/bitbucket/clguru/env/lib/python3.7/site-packages/s3transfer/futures.py in result(self)
71 # however if a KeyboardInterrupt is raised we want want to exit
72 # out of this and propogate the exception.
---> 73 return self._coordinator.result()
74 except KeyboardInterrupt as e:
75 self.cancel()
~/bitbucket/clguru/env/lib/python3.7/site-packages/s3transfer/futures.py in result(self)
231 # final result.
232 if self._exception:
--> 233 raise self._exception
234 return self._result
235
~/bitbucket/clguru/env/lib/python3.7/site-packages/s3transfer/tasks.py in _main(self, transfer_future, **kwargs)
253 # Call the submit method to start submitting tasks to execute the
254 # transfer.
--> 255 self._submit(transfer_future=transfer_future, **kwargs)
256 except BaseException as e:
257 # If there was an exception raised during the submission of task
~/bitbucket/clguru/env/lib/python3.7/site-packages/s3transfer/upload.py in _submit(self, client, config, osutil, request_executor, transfer_future, bandwidth_limiter)
547 # Determine the size if it was not provided
548 if transfer_future.meta.size is None:
--> 549 upload_input_manager.provide_transfer_size(transfer_future)
550
551 # Do a multipart upload if needed, otherwise do a regular put object.
~/bitbucket/clguru/env/lib/python3.7/site-packages/s3transfer/upload.py in provide_transfer_size(self, transfer_future)
324 fileobj.seek(0, 2)
325 end_position = fileobj.tell()
--> 326 fileobj.seek(start_position)
327 transfer_future.meta.provide_transfer_size(
328 end_position - start_position)
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py in seek(self, offset, whence)
1023 # Position is before the current position. Reset the ZipExtFile
1024
-> 1025 self._fileobj.seek(self._orig_compress_start)
1026 self._running_crc = self._orig_start_crc
1027 self._compress_left = self._orig_compress_size
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py in seek(self, offset, whence)
702 def seek(self, offset, whence=0):
703 with self._lock:
--> 704 if self.writing():
705 raise ValueError("Can't reposition in the ZIP file while "
706 "there is an open writing handle on it. "
AttributeError: '_SharedFile' object has no attribute 'writing'
If I comment out the lines after print(obj) to see the validate the zip file content,
import boto3
import io
import zipfile
import mimetypes
s3 = boto3.resource('s3')
service_zip = io.BytesIO()
service_bucket = s3.Bucket('services.readspeech.com')
build_bucket = s3.Bucket('servicesbuild.readspeech.com')
build_bucket.download_fileobj('servicesbuild.zip', service_zip)
with zipfile.ZipFile(service_zip) as myzip:
for nm in myzip.namelist():
obj = myzip.open(nm)
print(obj)
# service_bucket.upload_fileobj(obj,nm,
# ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
# service_bucket.Object(nm).Acl().put(ACL='public-read')
I see the following:
<zipfile.ZipExtFile name='favicon.ico' mode='r' compress_type=deflate>
<zipfile.ZipExtFile name='styles/main.css' mode='r' compress_type=deflate>
<zipfile.ZipExtFile name='images/example3.png' mode='r' compress_type=deflate>
<zipfile.ZipExtFile name='images/example1.png' mode='r' compress_type=deflate>
<zipfile.ZipExtFile name='images/example2.png' mode='r' compress_type=deflate>
<zipfile.ZipExtFile name='index.html' mode='r' compress_type=deflate>
Appears the issue is with python 3.7. I downgraded to python 3.6 and everything is fine. There is a bug reported on python 3.7
The misprint in the file lib/zipfile.py in line 704 leads to AttributeError: '_SharedFile' object has no attribute 'writing'
"self.writing()" should be replaced by "self._writing()". I also think this code should be covered by tests.
attribute 'writing
So to resolve the issue, use python 3.6.
On osx you can go back to Python 3.6 with the following command.
brew switch python 3.6.4_4

ipywidgets is not supporting in python terminal

I am trying to run below code using python terminal, it works completely fine when i run in jupyter notebook, but in ubuntu terminal it throws error. even tried running jupyter nbextension enable --py --sys-prefix widgetsnbextension on the terminal still I receive same error
import matplotlib as mp
from pylab import *
from sklearn import datasets
from ipywidgets import interact, widgets
from IPython.display import display, clear_output
faces = datasets.fetch_olivetti_faces()
class Trainer:
def __init__(self):
self.results = {}
self.imgs = faces.images
self.index = 0
def increment_face(self):
if self.index + 1 >= len(self.imgs):
return self.index
else:
while str(self.index) in self.results:
print(self.index)
self.index += 1
return self.index
def record_result(self, smile=True):
self.results[str(self.index)] = smile
trainer = Trainer()
button_smile = widgets.Button(description='smile')
button_no_smile = widgets.Button(description='sad face')
def display_face(face):
clear_output()
imshow(face, cmap='gray')
axis('off')
show()
def update_smile(b):
trainer.record_result(smile=True)
trainer.increment_face()
display_face(trainer.imgs[trainer.index])
def update_no_smile(b):
trainer.record_result(smile=False)
trainer.increment_face()
display_face(trainer.imgs[trainer.index])
button_no_smile.on_click(update_no_smile)
button_smile.on_click(update_smile)
display(button_smile)
display(button_no_smile)
display_face(trainer.imgs[trainer.index])
Traceback error when I was running from python terminal
Widget Javascript not detected. It may not be installed properly. Did you enable the widgetsnbextension? If not, then run "jupyter nbextension enable --py --sys-prefix widgetsnbextension"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/IPython/core/formatters.py in __call__(self, obj)
880 method = get_real_method(obj, self.print_method)
881 if method is not None:
--> 882 method()
883 return True
884
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _ipython_display_(self, **kwargs)
480 loud_error('The installed widget Javascript is the wrong version.')
481
--> 482 self._send({"method": "display"})
483 self._handle_displayed(**kwargs)
484
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _send(self, msg, buffers)
485 def _send(self, msg, buffers=None):
486 """Sends a message to the model in the front-end."""
--> 487 self.comm.send(data=msg, buffers=buffers)
488
489
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in send(self, data, metadata, buffers)
119 """Send a message to the frontend-side version of this comm"""
120 self._publish_msg('comm_msg',
--> 121 data=data, metadata=metadata, buffers=buffers,
122 )
123
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in _publish_msg(self, msg_type, data, metadata, buffers, **keys)
64 metadata = {} if metadata is None else metadata
65 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
---> 66 self.kernel.session.send(self.kernel.iopub_socket, msg_type,
67 content,
68 metadata=json_clean(metadata),
AttributeError: 'NoneType' object has no attribute 'session'
<ipywidgets.widgets.widget_button.Button at 0x7f0de5ebfb38>
Widget Javascript not detected. It may not be installed properly. Did you enable the widgetsnbextension? If not, then run "jupyter nbextension enable --py --sys-prefix widgetsnbextension"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/IPython/core/formatters.py in __call__(self, obj)
880 method = get_real_method(obj, self.print_method)
881 if method is not None:
--> 882 method()
883 return True
884
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _ipython_display_(self, **kwargs)
480 loud_error('The installed widget Javascript is the wrong version.')
481
--> 482 self._send({"method": "display"})
483 self._handle_displayed(**kwargs)
484
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipywidgets/widgets/widget.py in _send(self, msg, buffers)
485 def _send(self, msg, buffers=None):
486 """Sends a message to the model in the front-end."""
--> 487 self.comm.send(data=msg, buffers=buffers)
488
489
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in send(self, data, metadata, buffers)
119 """Send a message to the frontend-side version of this comm"""
120 self._publish_msg('comm_msg',
--> 121 data=data, metadata=metadata, buffers=buffers,
122 )
123
/home/harish/anaconda3/envs/facial_env/lib/python3.4/site-packages/ipykernel/comm/comm.py in _publish_msg(self, msg_type, data, metadata, buffers, **keys)
64 metadata = {} if metadata is None else metadata
65 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
---> 66 self.kernel.session.send(self.kernel.iopub_socket, msg_type,
67 content,
68 metadata=json_clean(metadata),
AttributeError: 'NoneType' object has no attribute 'session'
<ipywidgets.widgets.widget_button.Button at 0x7f0ddadc2cc0>
From user6764549 comments:
Ubuntu terminal cannot support interactive widgets. You need to run it in a "browser"-like or GUI enabled environment that can run Javascript.
Jupyter notebook is not the problem, Ubuntu terminal is the problem. It does not support displaying images or widgets. If you are only trying to avoid using a browser, you can look at Jupyter Qt console or Spyder.

Resources