Path: blob/master/Convolutional Neural Networks/dummy/__pycache__/dataset.cpython-36.pyc
24354 views
3
��Y�% � @ s@ d dl Zd dlZd dlmZ d dd�ZG dd� d�Zdd� ZdS )
� N)�cachec C s( |dkrt j| �d }t j|td�| S )a�
Generate the One-Hot encoded class-labels from an array of integers.
For example, if class_number=2 and num_classes=4 then
the one-hot encoded label is the float array: [0. 0. 1. 0.]
:param class_numbers:
Array of integers with class-numbers.
Assume the integers are from zero to num_classes-1 inclusive.
:param num_classes:
Number of classes. If None then use max(class_numbers)+1.
:return:
2-dim array of shape: [len(class_numbers), num_classes]
N� )�dtype)�np�max�eye�float)�
class_numbers�num_classes� r �#/home/jovyan/work/Resnet/dataset.py�one_hot_encoded s r
c @ s8 e Zd Zddd�Zdd� Zddd�Zd d
� Zdd� Zd
S )�DataSet�.jpgc C s t jj|�}|| _tdd� |D ��| _g | _g | _g | _g | _ g | _
d| _x�t j|�D ]�}t jj
||�}t jj|�rV| jj|� | j|�}| jj|� | j}|gt|� }| j j|� | jt jj
|d��}| jj|� |gt|� }| j
j|� | jd7 _qVW dS )a�
Create a data-set consisting of the filenames in the given directory
and sub-dirs that match the given filename-extensions.
For example, the knifey-spoony data-set (see knifey.py) has the
following dir-structure:
knifey-spoony/forky/
knifey-spoony/knifey/
knifey-spoony/spoony/
knifey-spoony/forky/test/
knifey-spoony/knifey/test/
knifey-spoony/spoony/test/
This means there are 3 classes called: forky, knifey, and spoony.
If we set in_dir = "knifey-spoony/" and create a new DataSet-object
then it will scan through these directories and create a training-set
and test-set for each of these classes.
The training-set will contain a list of all the *.jpg filenames
in the following directories:
knifey-spoony/forky/
knifey-spoony/knifey/
knifey-spoony/spoony/
The test-set will contain a list of all the *.jpg filenames
in the following directories:
knifey-spoony/forky/test/
knifey-spoony/knifey/test/
knifey-spoony/spoony/test/
See the TensorFlow Tutorial #09 for a usage example.
:param in_dir:
Root-dir for the files in the data-set.
This would be 'knifey-spoony/' in the example above.
:param exts:
String or tuple of strings with valid filename-extensions.
Not case-sensitive.
:return:
Object instance.
c s s | ]}|j � V qd S )N)�lower)�.0�extr r r � <genexpr>b s z#DataSet.__init__.<locals>.<genexpr>r �testr N)�os�path�abspath�in_dir�tuple�exts�class_names� filenames�filenames_testr �class_numbers_testr
�listdir�join�isdir�append�_get_filenames�extend�len) �selfr r �nameZcurrent_dirr Zclass_numberr r r r r �__init__6 s. &
zDataSet.__init__c C sB g }t jj|�r>x,t j|�D ]}|j� j| j�r|j|� qW |S )a
Create and return a list of filenames with matching extensions in the given directory.
:param dir:
Directory to scan for files. Sub-dirs are not scanned.
:return:
List of filenames. Only filenames. Does not include the directory.
)r r �existsr r �endswithr r"