File: MatDataSet.cc

package info (click to toggle)
torch3 3.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,968 kB
  • ctags: 2,743
  • sloc: cpp: 24,245; python: 299; makefile: 153
file content (226 lines) | stat: -rw-r--r-- 7,368 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (C) 2003--2004 Ronan Collobert (collober@idiap.ch)
//                
// This file is part of Torch 3.1.
//
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "IOBufferize.h"
#include "MatDataSet.h"
#include "IOAscii.h"
#include "IOMulti.h"
#include "IOBin.h"
#include "IOSub.h"

namespace Torch {

MatDataSet::MatDataSet(const char *filename, int n_inputs_, int n_targets_,
                       bool one_file_is_one_sequence, int max_load, bool binary_mode)
{
  io_allocator = new Allocator;
  if( (n_inputs_ < 0) && (n_targets < 0) )
    error("MatDataSet: cannot guess n_inputs <and> n_targets!");

  IOSequence *io_file = NULL;
  if(binary_mode)
    io_file = new(io_allocator) IOBin(filename, one_file_is_one_sequence, max_load);
  else
    io_file = new(io_allocator) IOAscii(filename, one_file_is_one_sequence, max_load);

  init_(io_file, n_inputs_, n_targets_);
}

MatDataSet::MatDataSet(char **filenames, int n_files_, int n_inputs_, int n_targets_,
                       bool one_file_is_one_sequence, int max_load, bool binary_mode)
{
  io_allocator = new Allocator;
  if(n_files_ <= 0)
    error("MatDataSet: check the number of files!");

  IOSequence **io_files = (IOSequence **)io_allocator->alloc(sizeof(IOSequence *)*n_files_);
  if(max_load > 0)
  {
    int i = 0;
    while( (max_load > 0) && (i < n_files_) )
    {
      if(binary_mode)
        io_files[i] = new(io_allocator) IOBin(filenames[i], one_file_is_one_sequence, max_load);
      else
        io_files[i] = new(io_allocator) IOAscii(filenames[i], one_file_is_one_sequence, max_load);
      max_load -= io_files[i]->n_sequences;
      i++;
    }
    n_files_ = i;
  }
  else
  {
    if(binary_mode)
    {
      for(int i = 0; i < n_files_; i++)
        io_files[i] = new(io_allocator) IOBin(filenames[i], one_file_is_one_sequence);
    }
    else
    {
      for(int i = 0; i < n_files_; i++)
        io_files[i] = new(io_allocator) IOAscii(filenames[i], one_file_is_one_sequence);
    }
  }

  IOMulti *io_file = new(io_allocator) IOMulti(io_files, n_files_);
  init_(io_file, n_inputs_, n_targets_);
}

MatDataSet::MatDataSet(char **input_filenames, char **target_filenames, int n_files_,
                       int max_load, bool binary_mode)
{
  IOSequence *io_inputs = NULL;
  IOSequence *io_targets = NULL;
  io_allocator = new Allocator;

  if(n_files_ <= 0)
    error("MatDataSet: check the number of files!");

  if(input_filenames)
  {
    IOSequence **input_io_files = (IOSequence **)io_allocator->alloc(sizeof(IOSequence *)*n_files_);
    int max_load_ = max_load;
    int n_files__ = 0;
    if(max_load_ > 0)
    {
      int i = 0;
      while( (max_load_ > 0) && (i < n_files_) )
      {
        if(binary_mode)
          input_io_files[i] = new(io_allocator) IOBin(input_filenames[i], true, max_load_);
        else
          input_io_files[i] = new(io_allocator) IOAscii(input_filenames[i], true, max_load_);
        max_load_ -= input_io_files[i]->n_sequences;
        i++;
      }
      n_files__ = i;
    }
    else
    {
      if(binary_mode)
      {
        for(int i = 0; i < n_files_; i++)
          input_io_files[i] = new(io_allocator) IOBin(input_filenames[i], true);
      }
      else
      {
        for(int i = 0; i < n_files_; i++)
          input_io_files[i] = new(io_allocator) IOAscii(input_filenames[i], true);
      }
      n_files__ = n_files_;
    }
    io_inputs = new(io_allocator) IOMulti(input_io_files, n_files__);
  }

  if(target_filenames)
  {
    IOSequence **target_io_files = (IOSequence **)io_allocator->alloc(sizeof(IOSequence *)*n_files_);
    int max_load_ = max_load;
    int n_files__ = 0;
    if(max_load_ > 0)
    {
      int i = 0;
      while( (max_load_ > 0) && (i < n_files_) )
      {
        if(binary_mode)
          target_io_files[i] = new(io_allocator) IOBin(target_filenames[i], true, max_load_);
        else
          target_io_files[i] = new(io_allocator) IOAscii(target_filenames[i], true, max_load_);
        max_load_ -= target_io_files[i]->n_sequences;
        i++;
      }
      n_files__ = i;
    }
    else
    {
      if(binary_mode)
      {
        for(int i = 0; i < n_files_; i++)
          target_io_files[i] = new(io_allocator) IOBin(target_filenames[i], true);
      }
      else
      {
        for(int i = 0; i < n_files_; i++)
          target_io_files[i] = new(io_allocator) IOAscii(target_filenames[i], true);
      }
      n_files__ = n_files_;
    }
    io_targets = new(io_allocator) IOMulti(target_io_files, n_files__);
  }

  MemoryDataSet::init(io_inputs, io_targets);
  message("MatDataSet: %d examples loaded [%d inputs and %d targets detected]", n_examples, n_inputs, n_targets);
  delete io_allocator;
}

void MatDataSet::init_(IOSequence *io_file, int n_inputs_, int n_targets_)
{
  IOSequence *io_inputs = NULL;
  IOSequence *io_targets = NULL;

  if( (n_inputs_ > io_file->frame_size) || (n_targets_ > io_file->frame_size) )
    error("MatDataSet: n_inputs (%d) or n_targets (%d) too large (> %d) !", n_inputs_, n_targets_, io_file->frame_size);

  if(n_inputs_ < 0)
    n_inputs_ = io_file->frame_size - n_targets_;

  if(n_targets_ < 0)
    n_targets_ = io_file->frame_size - n_inputs_;

  if(io_file->frame_size != (n_inputs_ + n_targets_))
    error("MatDataSet: %d columns in the file != %d inputs + %d targets", io_file->frame_size, n_inputs_, n_targets_);

  IOBufferize *io_buffer = NULL;
  if( (n_inputs_ > 0) && (n_targets_ > 0) )
    io_buffer = new(io_allocator) IOBufferize(io_file);

  if(n_inputs_ > 0)
  {
    if(n_targets_ > 0)
      io_inputs = new(io_allocator) IOSub(io_buffer, 0, n_inputs_);
    else
      io_inputs = io_file;
  }
  if(n_targets_ > 0)
  {
    if(n_inputs_ > 0)
      io_targets = new(io_allocator) IOSub(io_buffer, n_inputs_, n_targets_);
    else
      io_targets = io_file;
  }

  MemoryDataSet::init(io_inputs, io_targets);
  message("MatDataSet: %d examples loaded", n_examples);
  delete io_allocator;
}

MatDataSet::~MatDataSet()
{
}

}